2012-09-06 42 views
0

我已經構建了一個可以與標準設置一起工作的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.

什麼是錯我的代碼,會產生呢?它對我來說很好。

+0

和準確的例外是... –

+0

它不會告訴我。內部異常爲空。狀態是System.Net.WebExceptionStatus.ProtocolError並且消息是有用的:遠程服務器返回一個錯誤:(500)內部服務器錯誤。 – CSharpened

+0

該是在服務器中設置診斷的時候了。 –

回答

1

如果客戶端是.Net應用程序,您可以創建服務代理來調用它,如下所示。

Proxy.ServiceClient client = new Proxy.ServiceClient(); 
client.StoreNotice(GetMessage()); 

的方法getMessage,()

public static Message GetMessage() 
     { 
      //create an XML reader from the input XML 
      XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.Default.GetBytes(CreateSoapMessage("http://localhost:8732/Design_Time_Addresses/WCFCustomEndpointService/Service1/", "http://tempuri.org/IService1/StoreNotice", "content")))); 

      //create a WCF message from the XML reader 
      Message inputMessge = Message.CreateMessage(MessageVersion.Soap12, "http://tempuri.org/IService1/StoreNotice", reader); 

      return inputMessge; 
     } 

複製你的方法CreateSoapMessage

protected static 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); 
     } 
+0

出於某種原因,此代碼不會填充XmlReader對象。 – CSharpened

+0

你是否已經替換了視覺工作室設計時間網址? - http:// localhost:8732/Design_Time_Addresses /和http://tempuri.org/IService1/StoreNotice –

+0

謝謝,我最終得到了它。這不正是我想要的,但它的工作滿足了我的要求。 – CSharpened