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 屬性 (Attribute) 可讓您將含有 MessageContractAttribute 屬性 (Attribute) 標記之型別的欄位和屬性 (Property),對應至 SOAP 訊息標頭。 欄位或屬性可以是簡單型別,或可序列化的複合型別。

如需控制 SOAP 主體內容的序列化,而不需修改預設 SOAP 信封本身的資訊,請參閱 System.Runtime.Serialization.DataContractAttribute服務合約中指定資料傳輸,以及 使用資料合約

如需建立訊息合約的詳細資訊,請參閱 使用訊息合約

SOAP 標準會定義下列可存在於標頭的屬性:

  • ActorRole (在 SOAP 1.1 中為 Actor,在 SOAP 1.2 中為 Role)

  • MustUnderstand

  • Relay

ActorRole 屬性會指定節點的 URI,即為指定的標頭所指向的節點。 MustUnderstand 屬性會指定標頭是否能夠解讀節點處理。 Relay 屬性會指定標頭是否要轉送至下游節點。 Windows Communication Foundation (WCF) 不會對傳入訊息執行這些屬性的任何處理,但 屬性除外 MustUnderstand

不過,您還是可以讀取和寫入這些屬性,即使根據預設它們不會送出。 您可以用兩種方式設定屬性值。 首先,您可以變更 Actor 上的 MustUnderstandRelayMessageHeaderAttribute 屬性 (沒有 Role 屬性 - 設定 Actor 屬性,並在 Role 使用 SOAP 1.2 時傳遞) 。 例如:

控制這些屬性的另一種方式,是將需要的標頭型別變成 MessageHeader<T> 類別的型別參數,並將結果型別搭配 MessageHeaderAttribute 一起使用。 然後,以程式設計的方式使用 MessageHeader<T> 屬性 (Property),來設定 SOAP 屬性 (Attribute)。 例如:

如果同時使用動態和靜態控制項機制,則靜態設定採用預設值,但可以使用動態機制來覆寫。 例如:

允許使用動態屬性控制項來建立重複標頭。 例如:

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

在接收端,只能使用泛型 MessageHeader<T> 類別來讀取這些 SOAP 屬性。 您可以檢查 Actor 型別標頭的 RelayMustUnderstandMessageHeader<T> 屬性,探索接收到的訊息上的屬性設定。

接收到訊息並傳回訊息時,SOAP 屬性設定只會為 MessageHeader<T> 型別的標頭而往返。

建構函式

MessageHeaderAttribute()

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

屬性

Actor

取得或設定 URI,這個 URI 表示此標頭的目標節點。 使用 SOAP 1.2 時,會對應到 role 標頭屬性,而使用 SOAP 1.1 則會對應到 actor 標頭屬性。

HasProtectionLevel

在衍生類別中覆寫時,取得指出成員是否已指派保護層級的值。

(繼承來源 MessageContractMemberAttribute)
MustUnderstand

指定扮演 Actor 角色的節點是否必須解讀這個標頭。 這會對應到 mustUnderstand SOAP 標頭屬性。

Name

指定對應於這個成員的項目名稱。

(繼承來源 MessageContractMemberAttribute)
Namespace

指定對應於這個成員的項目命名空間。

(繼承來源 MessageContractMemberAttribute)
ProtectionLevel

指定成員要依現狀加以傳輸、簽署,或者加以簽署並加密。

(繼承來源 MessageContractMemberAttribute)
Relay

指定這個標頭是否要轉送至下游節點。 這會對應到 relay SOAP 標頭屬性。

TypeId

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

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

_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)

適用於