没有命名空间别名的 WCF 请求

WCF request without namespace alias

我有一个 WCF 服务,可以与 SvcUtil.exe 生成的测试客户端一起使用。 但我真正的客户是另一家使用不同技术的公司。

对于生成的测试客户端,XML 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <PostModel xmlns="http://tempuri.org/">
         <model xmlns:a="http://schemas.datacontract.org/2004/07/SomeName.Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:Items>
               <a:Item>
                  <a:SomeElement1>SomeValue</a:SomeElement1>
                  <a:SomeElement2>SomeValue</a:SomeElement2>
               </a:Item>
               <a:Item>
                  <a:SomeElement1>SomeValue</a:SomeElement1>
                  <a:SomeElement2>SomeValue</a:SomeElement2>
               </a:Item>
            </a:Items>
            <a:MoreItems>
               <a:MoreItem>
                  <a:SomeElement3>binary</a:SomeElement3>
               </a:MoreItem>
            </a:MoreItems>
         </model>
      </PostModel>
   </s:Body>
</s:Envelope>

但真正的客户端发送 xml 没有别名:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SOAP-ENV:Body>
      <PostModel xmlns="http://tempuri.org/">
         <model xmlns="http://schemas.datacontract.org/2004/07/SomeName.Models">
            <Items>
               <Item>
                  <SomeElement1>SomeValue</SomeElement1>
                  <SomeElement2>SomeValue</SomeElement2>
               </Item>
               <Item>
                  <SomeElement1>SomeValue</SomeElement1>
                  <SomeElement2>SomeValue</SomeElement2>
               </Item>
            </Items>
            <MoreItems>
               <MoreItem>
                  <SomeElement3>binary</SomeElement3>
               </MoreItem>
            </MoreItems>
         </model>
      </PostModel>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

结果是我在端点中收到的模型为 NULL。 如果我在 fiddler 中捕获请求并对其进行编辑,则只添加模型名称空间的别名并将其添加到它工作的子元素中。

这个问题有解决办法吗,据我所知客户端发送的XML是有效的XML,别名不是强制的。

测试客户端XML和真实客户端XML的区别在于元素modelnamespace。来自测试客户端:

  <PostModel xmlns="http://tempuri.org/">
     <model xmlns:a="http://schemas.datacontract.org/2004/07/SomeName.Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

这定义了一个具有本地名称 "model" 的元素。它没有名称空间前缀,因此存在于默认名称空间中。但是,它并没有为自己定义默认名称空间——而是定义了带有前缀 "a""i" 的名称空间。因此,它属于其父层次结构定义的任何默认名称空间,在本例中为 "http://tempuri.org/" 来自其直接父级。

在真实客户端的 XML 中,model 元素再次没有前缀,但 有自己的默认名称空间声明:

  <PostModel xmlns="http://tempuri.org/">
     <model xmlns="http://schemas.datacontract.org/2004/07/SomeName.Models">

因此 model 属于命名空间 "http://schemas.datacontract.org/2004/07/SomeName.Models" 而不是父级的默认命名空间。

model 的所有子元素在两个 XML 版本中都在同一个命名空间中。只有 model 元素本身不一致。

这种不一致可以解释端点中的空模型。