【发布时间】:2016-03-26 02:06:33
【问题描述】:
我正在与一个似乎不处理默认名称空间的 SOAP 服务进行交互,但它可以很好地处理在 SOAP 信封级别声明的全局名称空间和名称空间前缀。
问题在于 WCF 不会在根目录创建这些全局名称空间,而是使用显式的无前缀默认名称空间,服务显然会阻塞这些名称空间。现在我知道这并不是 WCF 的错 - 我相信 WCF 生成的消息是有效的 XML,但服务仍然会阻塞它。
使用 WCF 生成的输出如下所示:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:Security xmlns:h="http://docs.oasis-open.org/wss/2004/01/oasis-
...
</h:Security>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<cancelShipmentRequest xmlns="http://www.royalmailgroup.com/api/ship/V2">
<integrationHeader>
<dateTime xmlns="http://www.royalmailgroup.com/integration/core/V1">2016-03-26T01:44:37.0493801Z</dateTime>
<version xmlns="http://www.royalmailgroup.com/integration/core/V1">2</version>
<identification xmlns="http://www.royalmailgroup.com/integration/core/V1">
<applicationId>RMG-API-G-01</applicationId>
<transactionId>ozhckwej6sxg</transactionId>
</identification>
</integrationHeader>
<cancelShipments>
<shipmentNumber>TTT001908905GB</shipmentNumber>
</cancelShipments>
</cancelShipmentRequest>
</s:Body>
</s:Envelope>
这是行不通的。
使用以下 SOAP 信封(在 SoapUI 中手动)确实有效:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:oas="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:v2="http://www.royalmailgroup.com/api/ship/V2"
xmlns:v1="http://www.royalmailgroup.com/integration/core/V1">
<soapenv:Header>
<h:Security xmlns:h="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
...
</h:Security>
</soapenv:Header>
<soapenv:Body>
<v2:cancelShipmentRequest>
<v2:integrationHeader>
<v1:dateTime>2016-03-02T14:55:00Z</v1:dateTime>
<v1:version>2</v1:version>
<v1:identification>
<v1:applicationId>RMG-API-G-01</v1:applicationId>
<v1:transactionId>wftdaife96gv</v1:transactionId>
</v1:identification>
</v2:integrationHeader>
<v2:cancelShipments>
<v2:shipmentNumber>TTT001908905GB</v2:shipmentNumber>
</v2:cancelShipments>
</v2:cancelShipmentRequest>
</soapenv:Body>
</soapenv:Envelope>
两者的区别在于v1和v2命名空间是在文档顶部全局声明的,而第二个文档中没有局部命名空间声明。
也许我遗漏了一些东西,但对我来说,WCF 生成的 XML 看起来是有效的,并且在命名空间方面代表了相同的文档状态。
我能说的唯一区别是命名空间的声明方式。尽管 WCF 版本似乎是有效的并产生相同的命名空间,但该服务抱怨无效的命名空间引用。
架构验证失败:消息架构验证失败:架构有效性错误:元素“xmlns”:不需要此元素。预期为 ({http://www.royalmailgroup.com/api/ship/V2}integrationHeader)。
问题是,强制 WCF 在顶部而不是内联添加命名空间引用的最佳方法是什么?到目前为止,我发现的唯一方法是使用消息检查器并明确重写消息,但如果我经历了所有这些,我还不如手动创建消息。
有什么想法可以强制 WCF 在不手动重写消息的情况下使用显式命名空间前缀?
【问题讨论】:
-
我最近遇到了同样的情况,试图将现有的命名空间与新的命名空间合并。我无法给出明确的答案。我确实发现,在我删除了合并、重建、退出 VS,然后集成现有代码的尝试之后,一切都很好。蹩脚的答案,我同意。我得出的结论是它是 VS 中的错误,但过于复杂而无法尝试重现场景。祝你好运,我等待更好的解决方案/回答您的情况。
-
您尝试过this blog post或this answer中的建议吗?
-
谢谢理查德。这篇博文很有帮助,为我指明了正确的方向。