Customize SOAP Header namespace prefixes in WCF

眉间皱痕 提交于 2019-12-22 07:24:26

问题


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 ?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21495857/customize-soap-header-namespace-prefixes-in-wcf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!