WCF messageheader vs messagebodymember?

你离开我真会死。 提交于 2019-12-12 13:06:32

问题


I do not understand when I should put the [MessageHeader] instead of the [MessageBodyMember]?

I have read that it's used to conform some protocol like Soap, but still, what does it change at the end?


回答1:


SOAP is a protocol in which each message (request or response) is made up of two parts: the Header and the Body, inside an Envelope:

<s:Envelope xmlns:s='the namespace for the soap version'>
  <s:Header>
    <!-- SOAP headers will come here -->
  </s:Header>
  <s:Body>
    <!-- SOAP body members will come here -->
  </s:Body>
</s:Envelope>

You normally wouldn't use [MessageContract] (and MessageHeader or MessageBodyMember) in WCF services, only if you really need to interop with some 3rd party which expects the data in a certain format.




回答2:


I think when I want to put something independent of message content, I will put it in the message header. And if you want another party to read something from your message, it should be put in the header, because sometimes you may allow someone to read the message header not message body as it contains confidential contents.




回答3:


You can think about it as difference between message data (MessageBodyMember) and message metadata (MessageHeader). There is plenty of build in standardized headers provided by WCF which deals with addressing, security, reliable messaging, etc. In default WCF implementation it is related only to SOAP.

There is also general rule that in complex messaging architecture there can be intermediaries who read metadata and use them to some processing and message routing (they can even add additional metadata) but they should never interfere with message body (data). In case of security they can even not be able to read message body or some metadata (headers).

In case of WCF you are able to set different security requirements (none, signed, signed and encrypted) for each message header and for whole message body (WCF doesn't support separate security requirements for different body parts). That can also lead to some decision about using body member or header.

For example if you decide to make some custom authentication, or transferring some client information in each message you will probably create custom header for that but real data payload related to the operation will be part of message body.



来源:https://stackoverflow.com/questions/6618869/wcf-messageheader-vs-messagebodymember

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