【问题标题】:How to deserialize a soap request with a namespace prefix?如何反序列化带有命名空间前缀的soap请求?
【发布时间】:2014-09-29 10:00:13
【问题描述】:

第一个请求有效。第二个在 arg0 中返回一个空值。唯一的区别是命名空间前缀“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>

【问题讨论】:

  • 你的 WSDL 是什么样的?
  • 添加了我的服务当前生成的 WSDL

标签: c# xml web-services wcf soap


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果没有看到每个导入架构的完整 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 中,而在第二种情况下, 在正确的命名空间中,但 不是.

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

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

    HTH,

    【讨论】:

    • 确实有道理,可能是问题所在,但仍然无法更改请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 2011-01-21
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多