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

问题描述:

第一个请求起作用。第二个返回arg0中的空值。唯一的区别是命名空间前缀“ns2”。带有前缀的请求来自外部来源,无法更改。如何使用名称空间前缀反序列化soap请求?

<?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> 
+0

什么是您的WSDL样子? – tomasr 2014-09-29 10:28:40

+0

添加了我的服务当前生成的WSDL – user3185092 2014-09-29 11:42:06

我想你说的是WCF服务/客户端。因此,您必须使用行为IEndpointBehavior来扩展您的服务/客户端。你必须认识到IDispatchMessageInspector/IClientMessageInspector这将检查请求消息,并与所需的命名空间prifix纠正它。 Here是一个很好的解释。

没有看到完整的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中由隐式命名空间包含,而第二个,<putEvent>位于正确的命名空间中,但<arg0>不是。

因此客户实际上是产生了错误的请求,应当发出这样的:

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

HTH,

+0

它确实有意义,这可能是问题,但请求仍然无法更改。 – user3185092 2014-09-29 14:33:47