如何使用命名空间前缀反序列化soap请求?

时间:2014-09-29 10:00:13

标签: c# xml web-services wcf soap

第一个请求有效。第二个在arg0中返回null。唯一的区别是命名空间前缀" ns2"。带前缀的请求来自外部来源,无法更改。

<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><putEvent xmlns="http://xxx.xxx.x.xx/">
<arg0>test</arg0>
</putEvent></S:Body>
</S:Envelope>


<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:putEvent xmlns:ns2="http://xxx.xxx.x.xx/">
<arg0>test</arg0>
</ns2:putEvent></S:Body>
</S:Envelope>

如何妥善处理此类请求?目前处理它的代码是从wsdl生成的。

wsdl:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://swd.ws.siwcpr.wasko.pl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="MockService" targetNamespace="http://swd.ws.siwcpr.wasko.pl/">
<wsdl:types>
<xsd:schema targetNamespace="http://swd.ws.siwcpr.wasko.pl/Imports">
<xsd:import schemaLocation="http://192.168.5.77:8733/SWDMockService/?xsd=xsd2" namespace="http://swd.ws.siwcpr.wasko.pl/"/>
<xsd:import schemaLocation="http://192.168.5.77:8733/SWDMockService/?xsd=xsd0" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://192.168.5.77:8733/SWDMockService/?xsd=xsd1" namespace="http://schemas.datacontract.org/2004/07/Siwcpr.Simulators.Swd.MockService.SwdServiceRef"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="pingRequest">
<wsdl:part name="ping" element="tns:ping"/>
</wsdl:message>
<wsdl:message name="pingResponse">
<wsdl:part name="pingResponse" element="tns:pingResponse"/>
</wsdl:message>
<wsdl:message name="putEventRequest">
<wsdl:part name="putEvent" element="tns:putEvent"/>
</wsdl:message>
<wsdl:message name="putEventResponse">
<wsdl:part name="putEventResponse" element="tns:putEventResponse"/>
</wsdl:message>
<wsdl:message name="notifyDictionaryChangeRequest">
<wsdl:part name="notifyDictionaryChange" element="tns:notifyDictionaryChange"/>
</wsdl:message>
<wsdl:message name="notifyDictionaryChangeResponse">
<wsdl:part name="notifyDictionaryChangeResponse" element="tns:notifyDictionaryChangeResponse"/>
</wsdl:message>
<wsdl:portType name="IMockService">
<wsdl:operation name="ping">
<wsdl:input wsaw:Action="http://swd.ws.siwcpr.wasko.pl/WsdWsService/pingRequest" name="pingRequest" message="tns:pingRequest"/>
<wsdl:output wsaw:Action="http://swd.ws.siwcpr.wasko.pl/WsdWsService/pingResponse" name="pingResponse" message="tns:pingResponse"/>
</wsdl:operation>
<wsdl:operation name="putEvent">
<wsdl:input wsaw:Action="http://swd.ws.siwcpr.wasko.pl/WsdWsService/putEventRequest" name="putEventRequest" message="tns:putEventRequest"/>
<wsdl:output wsaw:Action="http://swd.ws.siwcpr.wasko.pl/WsdWsService/putEventResponse" name="putEventResponse" message="tns:putEventResponse"/>
</wsdl:operation>
<wsdl:operation name="notifyDictionaryChange">
<wsdl:input wsaw:Action="http://swd.ws.siwcpr.wasko.pl/WsdWsService/notifyDictionaryChangeRequest" name="notifyDictionaryChangeRequest" message="tns:notifyDictionaryChangeRequest"/>
<wsdl:output wsaw:Action="http://swd.ws.siwcpr.wasko.pl/WsdWsService/notifyDictionaryChangeResponse" name="notifyDictionaryChangeResponse" message="tns:notifyDictionaryChangeResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IMockService" type="tns:IMockService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="ping">
<soap:operation soapAction="http://swd.ws.siwcpr.wasko.pl/WsdWsService/pingRequest" style="document"/>
<wsdl:input name="pingRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="pingResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="putEvent">
<soap:operation soapAction="http://swd.ws.siwcpr.wasko.pl/WsdWsService/putEventRequest" style="document"/>
<wsdl:input name="putEventRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="putEventResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="notifyDictionaryChange">
<soap:operation soapAction="http://swd.ws.siwcpr.wasko.pl/WsdWsService/notifyDictionaryChangeRequest" style="document"/>
<wsdl:input name="notifyDictionaryChangeRequest">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="notifyDictionaryChangeResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="MockService">
<wsdl:port name="BasicHttpBinding_IMockService" binding="tns:BasicHttpBinding_IMockService">
<soap:address location="http://192.168.5.77:8733/SWDMockService/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

2 个答案:

答案 0 :(得分:0)

我想你在谈论WCF服务/客户端。因此,您必须使用行为 IEndpointBehavior 扩展您的服务/客户端。您必须实现 IDispatchMessageInspector / IClientMessageInspector ,它将检查请求消息并使用所需的命名空间prifix进行更正。 Here是一个很好的解释。

答案 1 :(得分:0)

没有看到每个导入架构的完整XSD,可能很难说,但我相信问题是这样的:

<S:Body><ns2:putEvent xmlns:ns2="http://xxx.xxx.x.xx/">
<arg0>test</arg0> <--- here
</ns2:putEvent></S:Body>

问题在于,在第一种可行的情况下,arg0也被隐式命名空间包含在http://xxx.xxx.x.xx中,而在第二种情况下,&lt; putEvent&gt;在正确的命名空间中,但是&lt; arg0&gt;不是。

所以客户端实际上是在生成错误的请求,应该发出这个:

<S:Body><ns2:putEvent xmlns:ns2="http://xxx.xxx.x.xx/">
<ns2:arg0>test</ns2:arg0> <--- here
</ns2:putEvent></S:Body>

HTH,