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

Cos*_*ană 5 c# wcf messagecontract

我已经写了一个定制的命名空间和命名空间前缀由WCF生成的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>
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

不同之处在于第一个标题的名称空间是"if"而不是"f".

有没有办法使用自定义MessageFormatter和自定义Message类来做到这一点?

Cos*_*ană 3

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

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

如果您对此有不同的解决方案,请发布它,因为将选择在不破坏 wcf 功能的情况下工作的更简单的答案。