配置WCF以text / xml发送请求并接收application / soap + xml响应

时间:2014-08-01 19:44:26

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

我想在 .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,当客户端排除与请求类型相同的text/xml时。 我尝试了wsHttpBindingwebHttpBinding绑定类型。但在这些情况下,服务返回<faultstring>Got no OperationType!</faultstring>,即这些绑定类型不适合该服务。

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

更新1: 在使用<customBinding>的@ YaronNaveh 建议之后,客户端能够解析服务响应。但即使有数据存在,我也会得到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。

1 个答案:

答案 0 :(得分: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。