【问题标题】:Configure WCF to send requests in text/xml and recieve application/soap+xml responses配置 WCF 以在 text/xml 中发送请求并接收 application/soap+xml 响应
【发布时间】:2014-08-01 19:44:26
【问题描述】:

我想在 .NET 应用程序中使用外部非 .NET SOAP 服务。
我可以通过 Visual Studio 中的“添加服务引用”使用其 WSDL 为该服务创建客户端,而不会出现任何警告或错误。
我有以下自动生成的配置:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="GenericTicketConnector_Service1" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://192.168.112.34/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnector"
        binding="basicHttpBinding" bindingConfiguration="GenericTicketConnector_Service1"
        contract="Otrs.TicketConnector.GenericTicketConnector_Interface"
        name="GenericTicketConnector_endPoint1" />
    </client>
  </system.serviceModel>

接下来,我编写通用客户端代码来测试我的客户端和服务之间的通信:

    try
    {
        var client = new GenericTicketConnector_InterfaceClient();
        var ticketSearchRequest = new OTRS_TicketSearch
        {
            ItemElementName = ItemChoiceType6.UserLogin,
            Item = "root@localhost",
            Password = "root",
            TicketChangeTimeNewerMinutes = "600"
        };

        var ticketSearchResult = client.TicketSearch(ticketSearchRequest);
    }
    catch (Exception ex)
    {
    }

在执行client.TicketSearch(ticketSearchRequest); 期间,我收到以下异常:

System.ServiceModel.ProtocolException was caught
  Message=The content type application/soap+xml; charset=UTF-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 577 bytes of the response were: '<?xml version="1.0" encoding="UTF-8"?><soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><TicketSearchResponse xmlns="http://www.otrs.org/TicketConnector/"><TicketID>981</TicketID><TicketID>980</TicketID><TicketID>979</TicketID><TicketID>978</TicketID><TicketID>977</TicketID></TicketSearchResponse></soap:Body></soap:Envelope>'.

AFAIK,此异常表明绑定错误。
该服务需要内容类型为text/xml 的请求,这就是为什么“添加服务参考”使用basicHttpBinding。但是由于某种原因,服务响应类型为application/soap+xml,当客户端expexts与请求类型相同的text/xml时。
我试过wsHttpBindingwebHttpBinding 绑定类型。但在这些情况下,服务返回&lt;faultstring&gt;Got no OperationType!&lt;/faultstring&gt;,即这些绑定类型不适合服务。

所以我的问题是:如何配置客户端绑定以发送内容类型为text/xml 的消息并使用application/soap+xml 中的响应?

更新 1: 在@YaronNaveh 建议与&lt;customBinding&gt; 之后,客户端能够解析服务响应。但即使响应中有数据,我也会毫无例外地得到ticketSearchResult == null
这是服务响应:

<soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <TicketSearchResponse xmlns="http://www.otrs.org/TicketConnector/">
         <TicketID>981</TicketID>
         <TicketID>980</TicketID>
         <TicketID>979</TicketID>
         <TicketID>978</TicketID>
      </TicketSearchResponse>
   </soap:Body>
</soap:Envelope>

here是我用来生成客户端的WSDL。

【问题讨论】:

    标签: c# .net web-services wcf soap


    【解决方案1】:

    首先使用此绑定检查请求中的 application/soap+xml 服务是否正常:

    <customBinding>
      <binding name="NewBinding0">
        <textMessageEncoding MessageVersion="Soap12" />
        <httpTransport />
      </binding>
    </customBinding>
    

    如果不是,您需要写一个custom message encoder。编码器实现可以与示例链接完全相同。您需要指示它接受任何内容类型:

    public override bool IsContentTypeSupported(string contentType)
    {
        return true;
    }
    

    示例配置已配置为在请求中发送soap/xml。

    【讨论】:

    • 感谢您的帮助!我已经使用您的绑定进行了测试,并确保该服务适用于 SOAP 1.2 请求。它返回一个包含数据的响应,但在.NET 客户端我得到ticketSearchResult == null 没有任何例外。进一步的想法是什么?
    • 这似乎是一个新的无关问题。请使用架构发布响应 soap 和 WSDL。
    • 我已经用服务响应和用于生成客户端的 WSDL 更新了问题。
    • 从同一个 WSDL 设置一个临时的 WCF 服务(这里有一个选项merill.net/2011/08/…)。从您的客户端调用此服务并查看它返回的 SOAP 与实际服务返回的 SOAP 有何不同。很可能在响应元素之一上有不同的命名空间,或者响应中预期有一些包装器元素。可能可以通过更改 WSDL(当然是在您这边)或代理来解决此问题。
    • 另外,您可以尝试使用旧的 .Net 2 服务(“添加 Web 引用”)而不是 WCF,它们在某些 XML 风格下工作得更好,而且您似乎不需要 WCF 安全性。 webservices20.blogspot.co.il/2008/10/…
    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 2011-06-07
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多