我已經構建了一個可以與標準設置一起工作的WCF服務,但後來我決定嘗試實現一個SOAP 1.2綁定。自從對web.config和客戶端代碼進行更改後,我發現我得到了一個協議異常,並且我不知道爲什麼或導致它的原因。請參閱下面的代碼。配置看起來對我來說是正確的,所以我只能猜測客戶端代碼中存在問題。任何想法多人歡迎:WCF協議錯誤。我錯過了什麼?
的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);
}
編輯
通過使用跟蹤查看我一直跟此信息:
The message with To 'http://localhost:56919/TmaNoticeToClusteredEntityWcfService.svc/StoreNotice' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.
什麼是錯我的代碼,會產生呢?它對我來說很好。
和準確的例外是... –
它不會告訴我。內部異常爲空。狀態是System.Net.WebExceptionStatus.ProtocolError並且消息是有用的:遠程服務器返回一個錯誤:(500)內部服務器錯誤。 – CSharpened
該是在服務器中設置診斷的時候了。 –