我已经构建了一个可以在标准设置下正常工作的 WCF 服务,但后来我决定尝试实现一个 SOAP 1.2 绑定。自从对 web.config 和客户端代码进行更改后,我现在发现我遇到了协议异常,我不知道为什么或什么原因导致它。请在下面查看我的代码。配置对我来说是正确的,所以我只能猜测客户端代码中存在问题。任何想法都非常受欢迎:
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Namespace.ServiceName" behaviorConfiguration="EToNServiceBehavior">
<endpoint name="EToNSoap12" address=""
binding="customBinding" bindingConfiguration="soap12"
bindingNamespace="http://www.wrcplc.co.uk/Schemas/ETON"
contract="Namespace.IInterfaceName" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="EToNServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="soap12">
<textMessageEncoding messageVersion="Soap12" />
<httpTransport />
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
界面
namespace TheNamespace
{
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
/// <summary>
/// An interface to describe the contract offered by a class of this type.
/// </summary>
[ServiceContract]
public interface IInterfaceName
{
/// <summary>
/// A method to receive an EtoN notice
/// </summary>
/// <param name="message">The EtoN message</param>
[OperationContract (Name = "StoreNotice")]
[WebInvoke (Method = "POST", UriTemplate = "StoreNotice")]
Message StoreNotice (Message message);
}
}
客户代码
public string CallPostMethod()
{
const string action = "StoreNotice";
TestNotice testNotice = new TestNotice();
const string url = "http://myIp:myPort/ServiceName.svc/StoreNotice";
string contentType = String.Format("application/soap+xml; charset=utf-8; action=\"{0}\"", action);
string xmlString = CreateSoapMessage(url, action, testNotice.NoticeText);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] bytesToSend = encoding.GetBytes(xmlString);
request.Method = "POST";
request.ContentLength = bytesToSend.Length;
request.ContentType = contentType;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytesToSend, 0, bytesToSend.Length);
requestStream.Close();
}
string responseFromServer;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
responseFromServer = reader.ReadToEnd();
}
dataStream.Close();
}
XDocument document = XDocument.Parse(responseFromServer);
return document.ToString();
}
catch(WebException e)
{
throw e;
}
}
protected string CreateSoapMessage(string url, string action, string messageContent)
{
return String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">
<soap12:Body>
{0}
</soap12:Body>
</soap12:Envelope>", messageContent, action, url);
}
编辑
通过使用跟踪查看器,我得到了以下信息:
由于 EndpointDispatcher 的 AddressFilter 不匹配,接收方无法处理带有 To 'http://localhost:56919/TmaNoticeToClusteredEntityWcfService.svc/StoreNotice' 的消息。检查发送方和接收方的 EndpointAddresses 是否一致。
我的代码会产生什么问题?在我看来没问题。