MessageHeaders 类

定义

表示消息的消息头集合。Represents a collection of message headers for a message. 此类不能被继承。This class cannot be inherited.

public ref class MessageHeaders sealed : System::Collections::Generic::IEnumerable<System::ServiceModel::Channels::MessageHeaderInfo ^>
public sealed class MessageHeaders : System.Collections.Generic.IEnumerable<System.ServiceModel.Channels.MessageHeaderInfo>
type MessageHeaders = class
    interface seq<MessageHeaderInfo>
    interface IEnumerable
Public NotInheritable Class MessageHeaders
Implements IEnumerable(Of MessageHeaderInfo)
继承
MessageHeaders
实现

示例

下面的代码示例演示一个基本客户端使用通道工厂发送消息并读取答复中的操作标头。The following code example demonstrates a basic client using the channel factory to send a message and read the action header from 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.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 header contains optional metadata that describe a message encapsulated by the Message class.

Message 可以没有标头或有一个或多个标头,这些标头作为一种扩展机制用来在特定于应用程序的消息中传递信息。A Message can have zero or more headers that are used as an extension mechanism to pass information in messages that are 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:

标头名称Header Name 说明Description
功能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(MessageHeaders)

使用指定的消息头集合初始化 MessageHeaders 类的新实例。Initializes a new instance of the MessageHeaders class with the specified collection of message headers.

MessageHeaders(MessageVersion)

使用指定的消息版本初始化 MessageHeaders 类的新实例。Initializes a new instance of the MessageHeaders class with the specified message version.

MessageHeaders(MessageVersion, Int32)

使用指定的消息版本和大小初始化 MessageHeaders 类的新实例。Initializes a new instance of the MessageHeaders class with the specified message version and size.

属性

Action

获取或设置应如何处理消息的说明。Gets or sets a description of how the message should be processed.

Count

获取此集合中的消息头数。Gets the number of message headers in this collection.

FaultTo

获取或设置错误应发送到的节点地址。Gets or sets the address of the node to which faults should be sent.

From

获取或设置发送消息的节点的地址。Gets or sets the address of the node that sent the message.

Item[Int32]

在给定的索引位置检索标头。Retrieves a header at the given index.

MessageId

获取或设置消息的唯一 ID。Gets or sets the unique ID of the message.

MessageVersion

获取消息的 SOAP 版本。Gets the SOAP version of the message.

RelatesTo

获取与此消息相关的消息的 ID。Gets the IDs of messages that are related to this message.

ReplyTo

获取或设置对请求的答复应发送到的节点的地址。Gets or sets the address of the node to which a reply should be sent for a request.

To

获取或设置消息的目标终结点。Gets or sets the destination endpoint of a message.

UnderstoodHeaders

获取必须按照 SOAP 1.1 和 1.2 规范解释的所有消息头。Gets all the message headers that must be understood, according to SOAP 1.1/1.2 specification.

方法

Add(MessageHeader)

将指定的消息头添加到集合。Adds the specified message header to the collection.

Clear()

从集合中移除所有标头。Removes all the headers from the collection.

CopyHeaderFrom(Message, Int32)

将位于指定索引位置的标头内容从指定的消息复制到此实例。Copies the header content located at the specified index from the specified message to this instance.

CopyHeaderFrom(MessageHeaders, Int32)

将位于指定索引位置的标头内容从指定的消息头集合复制到此实例。Copies the header content located at the specified index from the specified message header collection to this instance.

CopyHeadersFrom(Message)

将所有标头的内容从指定的消息复制到此实例。Copies the content of all the headers from the specified message to this instance.

CopyHeadersFrom(MessageHeaders)

将内容从指定的标头集合复制到此实例。Copies the content from the specified header collection to this instance.

CopyTo(MessageHeaderInfo[], Int32)

将标头从此集合复制到一个数组中,并从该数组的某个特定索引位置开始。Copies the headers from this collection to an array, starting at a particular index of the array.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
FindHeader(String, String)

使用标头元素的指定 LocalName 和命名空间 URI 在此集合中查找消息头。Finds a message header in this collection by the specified LocalName and namespace URI of the header element.

FindHeader(String, String, String[])

按标头元素的指定 LocalName、命名空间 URI 和操作者在此集合中查找消息头。Finds a message header in this collection by the specified LocalName, namespace URI and actors of the header element.

GetEnumerator()

返回迭代集合的一个枚举数。Returns an enumerator for iterating through the collection. 不能继承此方法。This method cannot be inherited.

GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetHeader<T>(Int32)

在此集合中检索位于特定位置的消息头。Retrieves a message header at a specific position in this collection.

GetHeader<T>(Int32, XmlObjectSerializer)

在此集合中检索位于特定位置的消息头。Retrieves a message header at a specific position in this collection.

GetHeader<T>(String, String)

使用标头元素的指定 LocalName 和命名空间 URI 在此集合中查找消息头。Finds a message header in this collection by the specified LocalName and namespace URI of the header element.

GetHeader<T>(String, String, String[])

按标头元素的指定 LocalName、命名空间 URI 和操作者在此集合中检索消息头。Retrieves a message header in this collection by the specified LocalName, namespace URI and actors of the header element.

GetHeader<T>(String, String, XmlObjectSerializer)

按指定 LocalName、命名空间 URI 和序列化程序在此集合中检索消息头。Retrieves a message header in this collection by the specified LocalName, namespace URI and serializer.

GetReaderAtHeader(Int32)

获取一个 XML 字典读取器,该读取器使用位于集合中指定位置的消息头。Gets a XML dictionary reader that consumes the message header at the specified location of the collection.

GetType()

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

(继承自 Object)
HaveMandatoryHeadersBeenUnderstood()

验证是否已正确解释和处理了所有标记为 MustUnderstand 的消息头。Verifies whether all the message headers marked with MustUnderstand have been properly interpreted and processed.

HaveMandatoryHeadersBeenUnderstood(String[])

验证指定接收方是否已正确解释和处理了所有标记为 MustUnderstand 的消息头。Verifies whether the specified recipients have properly interpreted and processed all the message headers marked with MustUnderstand.

Insert(Int32, MessageHeader)

将消息头插入到集合中的指定索引位置。Inserts a message header into the collection at the specified index.

MemberwiseClone()

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

(继承自 Object)
RemoveAll(String, String)

从集合中移除具有指定名称和命名空间的所有标头。Removes all headers with the specified name and namespace from the collection.

RemoveAt(Int32)

从集合中移除位于指定索引位置的消息头。Removes the message header at the specified index from the collection.

SetAction(XmlDictionaryString)

设置标头的操作元素。Sets the action element of the header.

ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)
WriteHeader(Int32, XmlDictionaryWriter)

使用指定 XML 编写器从指定位置序列化标头。Serializes the header from the specified location using the specified XML writer.

WriteHeader(Int32, XmlWriter)

使用指定 XML 编写器从指定位置序列化标头。Serializes the header from the specified location using the specified XML writer.

WriteHeaderContents(Int32, XmlDictionaryWriter)

使用指定的 XML 编写器序列化指定的标头内容。Serializes the specified header content using the specified XML writer.

WriteHeaderContents(Int32, XmlWriter)

使用指定 XML 编写器序列化指定的标头内容。Serializes the specified header content using the specified XML writer.

WriteStartHeader(Int32, XmlDictionaryWriter)

使用指定的 XML 编写器序列化开始标头。Serializes the start header using the specified XML writer.

WriteStartHeader(Int32, XmlWriter)

使用指定的 XML 编写器序列化开始标头。Serializes the start header using the specified XML writer.

显式界面实现

IEnumerable.GetEnumerator()

返回迭代集合的一个枚举数。Returns an enumerator for iterating through the collection. 不能继承此方法。This method cannot be inherited.

扩展方法

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个节点的上级。Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个节点的上级。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>)

返回元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName)

返回经过筛选的元素集合,其中包含源集合中每个元素和文档的子代元素。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>)

返回源集合中每个元素和文档的子元素的集合。Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName)

返回源集合中经过筛选的每个元素和文档的子元素集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中仅包括具有匹配 XName 的元素。Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>)

返回节点集合(其中包含源集合中的所有节点),并按文档顺序排列。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于