在WCF中自定义SOAP Header名称空间前缀

时间:2014-02-01 08:27:33

标签: c# wcf messagecontract

我写过一篇关于在wcf here生成的SOAP消息中自定义名称空间和名称空间前缀的方法。

但是,我找不到在Message类中重写的正确方法,以便自定义消息的SOAP标头。

我想传达这样的信息:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<h:Protocol xmlns="http://www.xyz.de/Protocol" xmlns:h="http://www.xzy.de/Protocol">
<version>IFD_1.4</version>
</h:Protocol>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</s:Body>
</s:Envelope>

看起来像这样:

<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<if:Protocol xmlns="http://www.xyz.de/Protocol" xmlns:if="http://www.xzy.de/Protocol">
<version>IFD_1.4</version>
</if:Protocol>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</s:Body>
</s:Envelope>

不同之处在于第一个标题的命名空间是“if”而不是“f”。

有没有办法使用带有自定义Message类的自定义MessageFormatter来执行此操作?

2 个答案:

答案 0 :(得分:3)

我找到的解决方案是从MessageHeader类派生并在我的自定义Message类中使用它(请参阅问题链接以查看我如何使用自定义Message类来自定义信封和正文开始标记)。

一旦准备好,我将用完整的例子更新答案。

如果您有不同的解决方案,请将其发布为更简单的答案,该答案在不破坏wcf功能的情况下将被选中。

答案 1 :(得分:0)

您可以使用自定义MessageEncoder,在ReadMessage和WriteMessage方法中,您可以使用xml执行任何操作。