WCF XML deserialization is being very picky about namespaces and aliases

我是研究僧i 提交于 2019-12-24 05:15:48

问题


I am writing a .net WCF SOAP service, trying to integrate with a Java client. The process is a bit unusual, because the client has specified the WSDL, and I have to create a service which can accept requests from them.

Im getting some very finicky results when we try to perform the integration. Very subtle differences (that appear to be ok to my eyes) cause the XML deserialization to fail.

This is the XML actually produced by the client, which ends up giving me null values for all the child properties of CCHNameSearchResponse for example, the InterfaceControlField which is set to "999" in the XML comes across empty)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:soapclient.cch.doj.state.wi.us">
   <soapenv:Header/>
   <soapenv:Body>
      <impl:CCHNameSearchResponse xmlns:impl="urn:soapclient.cch.doj.state.wi.us" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <InterfaceControlField>999</InterfaceControlField>

This one works, the only difference is the namespace alias (xmlns:impl=) which to my eyes looks like it acceptable?

xmlns:urn="urn:soapclient.cch.doj.state.wi.us">
   <soapenv:Header/>
   <soapenv:Body>
      <CCHNameSearchResponse xmlns="urn:soapclient.cch.doj.state.wi.us" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <InterfaceControlField xmlns="">999</InterfaceControlField>

WHY? How can I get this to accept the XML as produced?


回答1:


The issue is that without an alias, the namespace becomes the default namespace for all child elements.

With the alias, the child elements are in the "no-namespace", and therefore are not picked up correctly in the serialization.

This is per spec, so its a problem with the side generating the XML for me.

http://www.w3.org/TR/xml-names/#defaulting

So either no alias should be used (per the second example in the OP) or the alias must be used everywhere, as in the sample here

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:soapclient.cch.doj.state.wi.us">
   <soapenv:Header/>
   <soapenv:Body>
      <impl:CCHNameSearchResponse xmlns:impl="urn:soapclient.cch.doj.state.wi.us" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <impl:InterfaceControlField>999</impl:InterfaceControlField>



回答2:


I just had the same issue and I solved it by adding and empty namespace to the complex types that are part of the request.

ServiceContract(Namespace = "www.someDomainName.com")]
[XmlSerializerFormat]
public interface IExampleService
{
    [OperationContract]
    ServiceResponse TestCall(ServiceRequest request);
}

[MessageContract(WrapperNamespace = "www.someDomainName.com")]
public class ServiceRequest
{
    [MessageBodyMember (Name="CallID",Namespace="")]
    public CallID CallId { get; set; }
}

And then this gets properly deserialized:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:namespaceAlias="www.someDomainName.com">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <namespaceAlias:ServiceRequest >
      <CallID>
        <TransactionUID>r4werf</TransactionUID>
        <CallUID>fdsf</CallUID>
      </CallID>
    </namespaceAlias:ServiceRequest>
  </s:Body>
</s:Envelope>


来源:https://stackoverflow.com/questions/31440352/wcf-xml-deserialization-is-being-very-picky-about-namespaces-and-aliases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!