共用方式為


MessageHeaderAttribute 類別

定義

指定資料成員為 SOAP 訊息標頭。

public ref class MessageHeaderAttribute : System::ServiceModel::MessageContractMemberAttribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)]
public class MessageHeaderAttribute : System.ServiceModel.MessageContractMemberAttribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, AllowMultiple=false, Inherited=false)>]
type MessageHeaderAttribute = class
    inherit MessageContractMemberAttribute
Public Class MessageHeaderAttribute
Inherits MessageContractMemberAttribute
繼承
衍生
屬性

範例

以下程式碼範例展示了如何使用 該MessageHeaderAttribute標頭來建立回應訊息的 SOAP 標頭,並將NameNamespaceMustUnderstand屬性設定為適合該標頭的值。 程式碼範例後接著是發送訊息時的範例。

 [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 訊息標頭。 欄位或屬性可以是簡單型態,也可以是可以序列化的複合型態。

關於如何在不修改預設 SOAP 信封本身的情況下控制 SOAP 主體內容的序列化資訊,請參見 System.Runtime.Serialization.DataContractAttribute服務合約中的資料傳輸規範》及 《使用資料合約》。

欲了解更多關於建立訊息合約的資訊,請參閱 「使用訊息合約」。

SOAP 標準定義了標頭上可存在的以下屬性:

  • ActorRoleActor 在 SOAP 1.1 中,在 Role SOAP 1.2 中)

  • MustUnderstand

  • Relay

Actor or Role 屬性指定該標頭所指涉節點的 URI。 屬性 MustUnderstand 指定標頭是否理解節點處理。 Relay 屬性會指定標頭是否要轉送至下游節點。 Windows Communication Foundation(WCF)不會對收到的訊息執行任何屬性處理,除了屬性之外 MustUnderstand

不過,你可以讀寫這些屬性,即使它們不是預設傳送的。 屬性值有兩種設定方式。 首先,你可以更改 Actor上的 、 MustUnderstandRelay 和 屬性。MessageHeaderAttribute (沒有 Role 屬性——設定屬性 ActorRole 若使用 SOAP 1.2 則會傳入。) 例如:

控制這些屬性的第二種方法是將想要的標頭型別設為類別的型別參數 MessageHeader<T> ,並結合所得型別 MessageHeaderAttribute與 。 然後用 MessageHeader<T> 這些屬性程式化設定 SOAP 屬性。 例如:

若同時使用動態控制機制與靜態控制機制,靜態設定為預設,但可透過動態機制覆蓋。 例如:

允許建立帶有動態屬性控制的重複標頭。 例如:

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

在接收端,只有使用通用 MessageHeader<T> 類別時才能讀取這些 SOAP 屬性。 檢查ActorMessageHeader<T>類型標頭的屬性MustUnderstandRelay,以發現收到訊息的屬性設定。

當收到訊息後再回傳時,SOAP 屬性設定只會對該 MessageHeader<T> 類型的標頭進行往返。

建構函式

名稱 Description
MessageHeaderAttribute()

初始化 MessageHeaderAttribute 類別的新執行個體。

屬性

名稱 Description
Actor

取得或設定一個 URI,指示該標頭所鎖定的節點。 使用 SOAP 1.2 時,會對應到角色標頭屬性;使用 SOAP 1.1 時則映射到演員標頭屬性。

HasProtectionLevel

當在派生類別中覆寫時,會獲得一個值,表示該成員是否被指定了保護等級。

(繼承來源 MessageContractMemberAttribute)
MustUnderstand

指定執行該 Actor 角色的節點是否必須理解此標頭。 此屬性映射至 mustUnderstand SOAP 標頭屬性。

Name

指定對應該成員的元素名稱。

(繼承來源 MessageContractMemberAttribute)
Namespace

指定對應該成員的元素命名空間。

(繼承來源 MessageContractMemberAttribute)
ProtectionLevel

指定成員是要 as-is、簽署,還是簽署並加密傳送。

(繼承來源 MessageContractMemberAttribute)
Relay

指定此標頭是否要中繼至下游節點。 此屬性映射至 relay SOAP 標頭屬性。

TypeId

在衍生類別中實作時,取得這個 Attribute的唯一標識碼。

(繼承來源 Attribute)

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

明確介面實作

名稱 Description
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取 物件的型別資訊,可用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開屬性和方法的存取權。

(繼承來源 Attribute)

適用於