Customize SOAP Header namespace prefixes in WCF

2019-04-30 02:36发布

I have wrote about a way to customize namespaces and namespace prefixes in a SOAP message generated by wcf here.

However, I can't find a proper method to override in the Message class in order to customize the SOAP headers of the messages.

I want to make this message:

<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>

Look like this:

<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>

The difference is that the namespace of the first header is "if" instead of "f".

Is there any way to do this using a custom MessageFormatter with a custom Message class ?

2条回答
我只想做你的唯一
2楼-- · 2019-04-30 02:58

You could use a custom MessageEncoder and in the ReadMessage and WriteMessage methods you can do whatever you like with the xml.

查看更多
该账号已被封号
3楼-- · 2019-04-30 03:08

The solution I found is to derive from MessageHeader class and use it in my custom Message class (see link from question to see how I used the custom Message class to customize the envelope and body start tag).

I will update the answer with a full example once it will be ready.

If you have different solutions for this, please post it as the simpler answer which works without breaking the functionality of wcf will be selected.

查看更多
登录 后发表回答