MessageHeaderAttribute MessageHeaderAttribute MessageHeaderAttribute MessageHeaderAttribute Class

定义

指定数据成员为 SOAP 消息头。Specifies that a data member is a SOAP message header.

public ref class MessageHeaderAttribute : System::ServiceModel::MessageContractMemberAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)]
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field, AllowMultiple=false, Inherited=false)]
[System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field, Inherited=false)]
public class MessageHeaderAttribute : System.ServiceModel.MessageContractMemberAttribute
type MessageHeaderAttribute = class
    inherit MessageContractMemberAttribute
Public Class MessageHeaderAttribute
Inherits MessageContractMemberAttribute
继承
派生
属性

示例

下面的代码示例演示如何使用 MessageHeaderAttribute 创建响应消息的 SOAP 标头,其中 NameNamespaceMustUnderstand 属性设置为适合于此标头的值。The following code example shows the use of the MessageHeaderAttribute to create a SOAP header for the response message with the Name, Namespace and MustUnderstand properties set to values appropriate for this header. 此代码示例之后的示例为发送时的消息示例。The code example is followed by an example of the message when sent.

 [MessageContract]
 public class HelloResponseMessage
 {
   private string localResponse = String.Empty;
   private string extra = String.Empty;

   [MessageBodyMember(
     Name = "ResponseToGreeting",
     Namespace = "http://www.examples.com")]
   public string Response
   {
     get { return localResponse; }
     set { localResponse = value; }
   }

   [MessageHeader(
     Name = "OutOfBandData",
     Namespace = "http://www.examples.com",
     MustUnderstand=true
   )]
   public string ExtraValues
   {
     get { return extra; }
     set { this.extra = value; }
  }

  /*
   The following is the response message, edited for clarity.
   
   <s:Envelope>
     <s:Header>
       <a:Action s:mustUnderstand="1">http://HelloResponseMessage/Action</a:Action>
       <h:OutOfBandData s:mustUnderstand="1" xmlns:h="http://www.examples.com">Served by object 13804354.</h:OutOfBandData>
     </s:Header>
     <s:Body>
       <HelloResponseMessage xmlns="Microsoft.WCF.Documentation">
         <ResponseToGreeting xmlns="http://www.examples.com">Service received: Hello.</ResponseToGreeting>
       </HelloResponseMessage>
     </s:Body>    
   </s:Envelope>
   */
}
  <MessageContract> _
  Public Class HelloResponseMessage
	Private localResponse As String = String.Empty
	Private extra As String = String.Empty

	<MessageBodyMember(Name := "ResponseToGreeting", Namespace := "http://www.examples.com")> _
	Public Property Response() As String
	  Get
		  Return localResponse
	  End Get
	  Set(ByVal value As String)
		  localResponse = value
	  End Set
	End Property

	<MessageHeader(Name := "OutOfBandData", Namespace := "http://www.examples.com", MustUnderstand:=True)> _
	Public Property ExtraValues() As String
	  Get
		  Return extra
	  End Get
	  Set(ByVal value As String)
		  Me.extra = value
	  End Set
	End Property

'   
'    The following is the response message, edited for clarity.
'    
'    <s:Envelope>
'      <s:Header>
'        <a:Action s:mustUnderstand="1">http://HelloResponseMessage/Action</a:Action>
'        <h:OutOfBandData s:mustUnderstand="1" xmlns:h="http://www.examples.com">Served by object 13804354.</h:OutOfBandData>
'      </s:Header>
'      <s:Body>
'        <HelloResponseMessage xmlns="Microsoft.WCF.Documentation">
'          <ResponseToGreeting xmlns="http://www.examples.com">Service received: Hello.</ResponseToGreeting>
'      </s:Body>    
'    </s:Envelope>
'    
  End Class

注解

MessageHeaderAttribute 特性允许您将用 MessageContractAttribute 特性标记的类型的字段和属性映射到 SOAP 消息头。The MessageHeaderAttribute attribute allows you to map fields and properties of a type marked with the MessageContractAttribute attribute to SOAP message headers. 该字段或属性可以是简单类型,也可以是可序列化的复合类型。The field or property can be of a simple type or a composite type that can be serialized.

不修改默认 SOAP 信封本身的情况下控制 SOAP 正文内容的序列化的信息,请参阅System.Runtime.Serialization.DataContractAttributeSpecifying Data Transfer in Service Contracts,和使用数据协定For information on controlling the serialization of the contents of a SOAP body without modifying the default SOAP envelope itself, see System.Runtime.Serialization.DataContractAttribute, Specifying Data Transfer in Service Contracts, and Using Data Contracts.

有关创建消息协定的详细信息,请参阅Using Message ContractsFor more information about creating message contracts, see Using Message Contracts.

SOAP 标准定义下列可在标头中存在的属性:The SOAP standard defines the following attributes that can exist on a header:

  • ActorRole(SOAP 1.1 中为 Actor,SOAP 1.2 中为 Role)。Actor or Role (Actor in SOAP 1.1, Role in SOAP 1.2)

  • MustUnderstand

  • Relay

ActorRole 属性指定需要给定标头的节点 URI。The Actor or Role attribute specifies the URI of the node for which a given header is intended. MustUnderstand 属性指定标头是否理解节点的处理。The MustUnderstand attribute specifies whether the header understands the node processing. Relay 特性指定标头是否要中继到下游节点。The Relay attribute specifies whether the header is to be relayed to downstream nodes. Windows Communication Foundation (WCF) 不会执行对传入的消息,这些属性的任何处理除MustUnderstand属性。Windows Communication Foundation (WCF) does not perform any processing of these attributes on incoming messages, except for the MustUnderstand attribute.

但是,即使默认情况下不发送这些属性,您也可以对它们进行读写。You can, however, read and write these attributes, even though they are not sent by default. 可以通过以下两种方式设置特性值。You can set the attribute values in two ways. 首先,可以在 Actor 中更改 MustUnderstandRelayMessageHeaderAttribute 属性。First, you can change the Actor, MustUnderstand and Relay properties on the MessageHeaderAttribute. (没有任何Role属性的设置Actor属性和Role如果使用 SOAP 1.2 传递)。(There is no Role property - set the Actor property and Role is passed if SOAP 1.2 is used). 例如:For example:

控制这些属性的第二种方式是将所需标头类型设为 MessageHeader<T> 类的类型参数,然后将生成的类型与 MessageHeaderAttribute 一起使用。The second way to control these attributes is by making the desired header type the type parameter of the MessageHeader<T> class and using the resulting type together with the MessageHeaderAttribute. 然后,再以编程方式使用 MessageHeader<T> 属性设置这些 SOAP 特性。Then use the MessageHeader<T> properties programmatically to set the SOAP attributes. 例如:For example:

如果同时使用了动态和静态控制机制,则静态设置为默认值,但可以使用动态机制重写。If both the dynamic and the static control mechanisms are used, the static settings are the default but can be overridden using the dynamic mechanism. 例如:For example:

允许使用动态属性控件创建重复的标头。Creating repeated headers with dynamic attribute control is allowed. 例如:For example:

[MessageHeaderArray] public MessageHeader<Person> documentApprovers[];  

在接收端,仅当使用泛型 MessageHeader<T> 类时才能读取这些 SOAP 属性。On the receiving side, reading these SOAP attributes can only be done if the generic MessageHeader<T> class is used. 检查 Actor 类型标头的 RelayMustUnderstandMessageHeader<T> 属性以发现已接收消息的属性设置。Examine the Actor, Relay or MustUnderstand properties of a header of the MessageHeader<T> type to discover the attribute settings on the received message.

如果在接收一条消息后又发送回发送方,则仅对 MessageHeader<T> 类型的标头往返 SOAP 属性设置。When a message is received and then sent back, the SOAP attribute settings only roundtrip for headers of the MessageHeader<T> type.

构造函数

MessageHeaderAttribute() MessageHeaderAttribute() MessageHeaderAttribute() MessageHeaderAttribute()

初始化 MessageHeaderAttribute 类的新实例。Initializes a new instance of the MessageHeaderAttribute class.

属性

Actor Actor Actor Actor

获取或设置一个指示此标头为目标的节点的 URI。Gets or sets a URI that indicates the node at which this header is targeted. 使用 SOAP 1.2 时映射到角色标头属性,而使用 SOAP 1.1 时则映射到参与者标头属性。Maps to the role header attribute when SOAP 1.2 is used and the actor header attribute when SOAP 1.1 is used.

HasProtectionLevel HasProtectionLevel HasProtectionLevel HasProtectionLevel

当在派生类中重写时,获取一个指示成员是否具有分配的保护级别的值。When overridden in a derived class, gets a value that indicates whether the member has a protection level assigned.

(Inherited from MessageContractMemberAttribute)
MustUnderstand MustUnderstand MustUnderstand MustUnderstand

指定在 Actor 角色中起作用的节点是否必须理解此标头。Specifies whether the node acting in the Actor role must understand this header. 这将映射到 mustUnderstand SOAP 标头属性。This is mapped to the mustUnderstand SOAP header attribute.

Name Name Name Name

指定与此成员对应的元素的名称。Specifies the name of the element that corresponds to this member.

(Inherited from MessageContractMemberAttribute)
Namespace Namespace Namespace Namespace

指定与此成员对应的元素的命名空间。Specifies the namespace of the element that corresponds to this member.

(Inherited from MessageContractMemberAttribute)
ProtectionLevel ProtectionLevel ProtectionLevel ProtectionLevel

指定该成员是否按原样传输,是否已签名或是否已签名和加密。Specifies whether the member is to be transmitted as-is, signed, or signed and encrypted.

(Inherited from MessageContractMemberAttribute)
Relay Relay Relay Relay

指定此标头是否要中继至下游节点。Specifies whether this header is to be relayed to downstream nodes. 这将映射到 relay SOAP 标头属性。This is mapped to the relay SOAP header attribute.

TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个值,该值指示此实例是否与指定的对象相等。Returns a value that indicates whether this instance is equal to a specified object.

(Inherited from Attribute)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此实例的哈希代码。Returns the hash code for this instance.

(Inherited from Attribute)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute()

在派生类中重写时,指示此实例的值是否是派生类的默认值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(Inherited from Attribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

适用于