Message.Headers Message.Headers Message.Headers Message.Headers Property

定义

在派生类中重写时,获取消息的标头。When overridden in a derived class, gets the headers of the message.

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 对象。A MessageHeaders object that represents the headers of the message.

异常

示例

下面的代码示例演示了使用通道工厂发送消息并读取回复的客户端。The following code example shows a client that uses the channel factory to send a message and read the reply.

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
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 可以没有标头或有一个或多个标头,这些标头作为一种扩展机制用来在应用程序特定的消息中传递信息。A Message can have zero or more headers that are used as an extension mechanism to pass information in messages that is application-specific. 通过调用 Headers 方法,可使用 Add 将消息头添加到消息中。You can use Headers to add message headers to a message by calling the Add method.

下表中所示,Windows Communication Foundation (WCF) 提供了一系列预定义的消息标头。Windows Communication Foundation (WCF) provides a number of predefined message headers, as shown in the following table.

标头名称Header Name 包含标头名称。Contains the header name.
功能To 包含作为消息投递目标的角色。Contains the role that the message is targeting.
操作Action 提供应如何处理消息的说明。Provides a description of how the message should be processed.
FaultToFaultTo 包含错误应发送到的节点的地址。Contains the address of the node to which faults should be sent.
FromFrom 包含发送消息的节点的地址。Contains the address of the node that sent the message.
请求Request 指示消息是否为请求。Indicates whether the message is a request.
MessageIDMessageID 包含消息的唯一 ID。Contains the unique ID of the message.
RelatesToRelatesTo 包含与此消息相关的消息的 ID。Contains the IDs of messages that are related to this message.
ReplyToReplyTo 包含对请求的答复应发送到的节点的地址。Contains the address of the node to which a reply should be sent for a request.

继承者说明

在派生中重写时,此方法为消息头返回 MessageHeaders 对象。When overridden in a derived class, this method returns a MessageHeaders object for the headers of the message.

适用于