【发布时间】: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时。
我试过wsHttpBinding 和webHttpBinding 绑定类型。但在这些情况下,服务返回<faultstring>Got no OperationType!</faultstring>,即这些绑定类型不适合服务。
所以我的问题是:如何配置客户端绑定以发送内容类型为text/xml 的消息并使用application/soap+xml 中的响应?
更新 1:
在@YaronNaveh 建议与<customBinding> 之后,客户端能够解析服务响应。但即使响应中有数据,我也会毫无例外地得到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