WCF协议错误。我错过了什么?

时间:2012-09-06 09:26:18

标签: c# wcf soap

我构建了一个可以正常使用标准设置的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);
    }

修改

通过使用跟踪查看器,我收到了以下信息:

  

带有To的消息   的 'http://本地主机:56919 / TmaNoticeToClusteredEntityWcfService.svc / StoreNotice'   由于AddressFilter不匹配,无法在接收器处理   在EndpointDispatcher。检查发件人和收件人   EndpointAddresses同意。

我的代码会产生什么问题?它看起来不错。

1 个答案:

答案 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);
        }