Message.Headers 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在衍生類別中覆寫時,會取得訊息的標頭。
public:
abstract property System::ServiceModel::Channels::MessageHeaders ^ Headers { System::ServiceModel::Channels::MessageHeaders ^ get(); };
public abstract System.ServiceModel.Channels.MessageHeaders Headers { get; }
member this.Headers : System.ServiceModel.Channels.MessageHeaders
Public MustOverride ReadOnly Property Headers As MessageHeaders
屬性值
MessageHeaders 物件,表示訊息的標頭。
例外狀況
訊息已經過處置。
範例
下列程式碼範例會示範使用通道處理站傳送訊息和讀取回覆的用戶端。
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
namespace ConsoleApplication1
{
class client
{
static void RunClient()
{
//Step1: create a binding with just HTTP
CustomBinding binding = new CustomBinding();
binding.Elements.Add(new HttpTransportBindingElement());
//Step2: use the binding to build the channel factory
IChannelFactory<IRequestChannel> factory =
binding.BuildChannelFactory<IRequestChannel>(
new BindingParameterCollection());
//open the channel factory
factory.Open();
//Step3: use the channel factory to create a channel
IRequestChannel channel = factory.CreateChannel(
new EndpointAddress("http://localhost:8080/channelapp"));
channel.Open();
//Step4: create a message
Message requestmessage = Message.CreateMessage(
MessageVersion.Soap12WSAddressing10,
"http://contoso.com/someaction",
"This is the body data");
//send message
Message replymessage = channel.Request(requestmessage);
Console.WriteLine("Reply message received");
Console.WriteLine("Reply action: {0}",
replymessage.Headers.Action);
string data = replymessage.GetBody<string>();
Console.WriteLine("Reply content: {0}", data);
//Step5: don't forget to close the message
requestmessage.Close();
replymessage.Close();
//don't forget to close the channel
channel.Close();
//don't forget to close the factory
factory.Close();
}
public static void Main()
{
Console.WriteLine("Press [ENTER] when service is ready");
Console.ReadLine();
RunClient();
Console.WriteLine("Press [ENTER] to exit");
Console.ReadLine();
}
}
}
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Runtime.Serialization
Namespace ConsoleApplication1
Friend Class client
Private Shared Sub RunClient()
'Step1: create a binding with just HTTP
Dim binding As New CustomBinding()
binding.Elements.Add(New HttpTransportBindingElement())
'Step2: use the binding to build the channel factory
Dim factory As IChannelFactory(Of IRequestChannel) = binding.BuildChannelFactory(Of IRequestChannel)(New BindingParameterCollection())
'open the channel factory
factory.Open()
'Step3: use the channel factory to create a channel
Dim channel As IRequestChannel = factory.CreateChannel(New EndpointAddress("http://localhost:8080/channelapp"))
channel.Open()
'Step4: create a message
Dim requestmessage As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "http://contoso.com/someaction", "This is the body data")
'send message
Dim replymessage As Message = channel.Request(requestmessage)
Console.WriteLine("Reply message received")
Console.WriteLine("Reply action: {0}", replymessage.Headers.Action)
Dim data = replymessage.GetBody(Of String)()
Console.WriteLine("Reply content: {0}", data)
'Step5: don't forget to close the message
requestmessage.Close()
replymessage.Close()
'don't forget to close the channel
channel.Close()
'don't forget to close the factory
factory.Close()
End Sub
Public Shared Sub Main()
Console.WriteLine("Press [ENTER] when service is ready")
Console.ReadLine()
RunClient()
Console.WriteLine("Press [ENTER] to exit")
Console.ReadLine()
End Sub
End Class
End Namespace
備註
Message 可以有零或多個標頭 (當做擴充機制使用),以便在應用程式特定的訊息中傳遞資訊。 您可以藉由呼叫 Headers 方法,使用 Add 將訊息標頭新增至訊息中。
Windows Communication Foundation (WCF) 提供一些預先定義的訊息標頭,如下表所示。
標頭名稱 | 包含標頭名稱。 |
---|---|
收件者 | 包含此訊息所針對的角色。 |
動作 | 提供應該如何處理此訊息的說明。 |
FaultTo | 包含錯誤應該傳送之目標節點的位址。 |
寄件者 | 包含傳送訊息之節點的位址。 |
要求 | 指出此訊息是否為要求。 |
MessageID | 包含此訊息的唯一 ID。 |
RelatesTo | 包含與此訊息相關之訊息的 ID。 |
ReplyTo | 包含應該針對要求傳送回覆之目標節點的位址。 |
給實施者的注意事項
在衍生類別中覆寫時,這個方法會為訊息的標頭傳回 MessageHeaders 物件。