简体   繁体   English

WCF messageheader vs messagebodymember?

[英]WCF messageheader vs messagebodymember?

I do not understand when I should put the [MessageHeader] instead of the [MessageBodyMember]?我不明白什么时候应该放置 [MessageHeader] 而不是 [MessageBodyMember]?

I have read that it's used to conform some protocol like Soap, but still, what does it change at the end?我已经读过它用于符合某些协议,例如 Soap,但是,它最后有什么变化?

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 : SOAP 是一个协议,其中每个消息(请求或响应)由两部分组成: HeaderBody ,在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.您通常不会在 WCF 服务中使用 [MessageContract](以及 MessageHeader 或 MessageBodyMember),除非您确实需要与某些期望数据采用某种格式的第三方进行互操作。

I think when I want to put something independent of message content, I will put it in the message header.我想当我想放一些独立于消息内容的东西时,我会把它放在消息 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.如果您希望另一方从您的消息中读取某些内容,则应将其放在 header 中,因为有时您可能会允许某人阅读消息 header 而不是消息正文,因为它包含机密内容。

You can think about it as difference between message data ( MessageBodyMember ) and message metadata ( MessageHeader ).您可以将其视为消息数据 ( MessageBodyMember ) 和消息元数据 ( 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. WCF 提供了大量内置标准化标头,用于处理寻址、安全性、可靠消息传递等。在默认的 WCF 实现中,它仅与 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).对于 WCF,您可以为每条消息 header 和整个消息正文设置不同的安全要求(无、签名、签名和加密)(WCF 不支持针对不同正文部分的单独安全要求)。 That can also lead to some decision about using body member or header.这也可能导致一些关于使用主体成员或 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.例如,如果您决定进行一些自定义身份验证,或者在每条消息中传输一些客户端信息,您可能会为此创建自定义 header,但与操作相关的实际数据有效负载将是消息正文的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM