Message Message Message Message Class

定义

表示分布式环境中终结点之间的通信单元。Represents the unit of communication between endpoints in a distributed environment.

public ref class Message abstract : IDisposable
public abstract class Message : IDisposable
type Message = class
    interface IDisposable
Public MustInherit Class Message
Implements IDisposable
继承
MessageMessageMessageMessage
实现

示例

下面的代码示例演示了使用通道工厂发送消息并读取回复的客户端。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 类提供一种方法,使网络中的发送方和接收方之间可对任意信息进行通信。The Message class provides a means of communicating arbitrary information between a sender and a receiver on a network. 使用它可以传递信息、建议或要求执行一系列操作或请求数据。It can be used to relay information, suggest or demand a course of action, or request data.

Message 对象的结构表示的是 SOAP 信封。The structure of a Message object represents a SOAP envelope. 它由两个不同的部分组成:消息的正文和一个可选择的标头集合,由 Headers 类表示。It consists of two distinct parts: the message's body and an optional collection of headers, represented by the Headers class. 消息内容是发送方发送给接收方的应用程序定义数据。The message content is application-defined data sent from a sender to a receiver. 消息头使系统和应用程序具有了扩展性来满足变化的要求,因为您可以定义代码来操作和响应特定标头。The message headers enable system and application extensibility to meet the changing requirements, because you can define code to manipulate and respond to specific headers. 您也可以定义自己的标头。You can also define your own headers. 消息头与消息内容一起进行序列化或反序列化。Message headers are serialized or deserialized along with the contents of the message.

以特别的格式接收和发送消息。Messages are received and sent in particular formats. 支持两种格式:基于文本的标准 XML 格式和基于二进制的 XML 格式。Support is provided for two formats: the standard text-based XML format and a binary-based XML format. Message 对象可用于表示 SOAP 1.1 和 SOAP 1.2 信封。The Message object can be used to represent both SOAP 1.1 and SOAP 1.2 envelopes. 请注意,Message 的实例在创建时已固定,并绑定到特定的 SOAP 版本。Note that an instance of Message is fixed upon creation and is bound to a specific SOAP version. Version 属性表示消息的 SOAP 版本。The Version property represents the SOAP version of the message.

使用 Message 方法可将 WriteMessage 对象序列化到外部存储区。A Message object can be serialized to an external store by using the WriteMessage method. 也可将消息的属性序列化,但需要对它们单独进行标识并分别进行序列化。Properties of the message can also be serialized, but they have to be individually identified and serialized separately. 使用 Message,可反序列化消息来创建一个内存中 CreateMessage 对象。Deserializing a message to create an in-memory Message object can be done using CreateMessage. 针对特定的 Message 实例,属性也必须是单独进行反序列化,并手动添加到属性集合中。Properties must also be deserialized individually and manually added to the property collection for the specific Message instance.

Message 对象的大小也固定为其传输的数据的大小。The size of a Message object is fixed to the size of data it is transmitting. 每个正文都建模为 XmlReader 的实例,对于 XmlReader 实例包装的流的大小没有预定义限制。Every body is modeled as an instance of XmlReader, with no predefined limit on the size of the stream that the XmlReader instance is wrapping. 但是,特定的通道提供程序可以对其处理的消息的大小做出限制。However, specific channel providers can have a limit on the size of messages that they process.

使用一个已检查和处理了消息的实体所生成的有用信息可批注 MessageA Message can be annotated with useful information generated by an entity that has examined and processed the message. HeadersProperties 属性提供了此功能。This functionality is provided by the Headers and Properties properties. Headers 集合表示消息上的 SOAP 标头集。The Headers collection represents the set of SOAP headers on the message.

Properties 属性表示消息上的处理级别批注集。The Properties property represents the set of processing-level annotations on the message. 由于标头中的信息通过网络传输,检查标头的实体必须支持标头使用的协议的基础版本。Because information in headers is transmitted on the wire, an entity that examines a header must support the underlying version(s) of the protocols used by the header. 但是,属性提供一种与版本更加无关的方式来批注消息。However, properties provide a more version-independent way of annotating a message.

若要创建 Message 实例,请使用其中一个 CreateMessage 方法。To create a Message instance, use one of the CreateMessage methods.

建议消息使用者使用完消息内容后,总是调用 CloseIt is recommended that a consumer of a message always call Close when the consumer is finished accessing the contents of the message. 此操作可释放与消息生存期相关联的有限系统资源(如套接字、命名管道)。This action frees finite system resources (for example, sockets, named pipes) that are tied to the lifetime of the message.

针对从此类派生的托管 C++ 用户的特别说明:Special note for Managed C++ users deriving from this class:

  • 将清理代码放入 (On)(Begin)Close(和/或 OnAbort)中,而不是放入析构函数中。Put your cleanup code in (On)(Begin)Close (and/or OnAbort), not in a destructor.

  • 避免使用析构函数:它们会导致编译器自动生成 IDisposableAvoid destructors: they cause the compiler to auto-generate IDisposable.

  • 避免使用非引用成员:它们可能会导致编译器自动生成 IDisposableAvoid non-reference members: they can cause the compiler to auto-generate IDisposable.

  • 避免使用终结器;但如果包含一个终结器,则应该禁止显示生成警告并从 (On)(Begin)Close(和/或 OnAbort)调用 SuppressFinalize(Object) 和终结器本身,以便模拟自动生成的 IDisposable 行为。Avoid finalizers; but if you include one, suppress the build warning and call SuppressFinalize(Object) and the finalizer itself from (On)(Begin)Close (and/or OnAbort) to emulate what would have been the auto-generated IDisposable behavior.

继承者说明

当从 Message 继承时,必须重写下面的成员:HeadersVersionWhen you inherit from Message, you must override the following members: Headers, and Version.

构造函数

Message() Message() Message() Message()

初始化 Message 类的新实例。Initializes a new instance of the Message class.

属性

Headers Headers Headers Headers

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

IsDisposed IsDisposed IsDisposed IsDisposed

返回一个值,该值指示是否已释放 MessageReturns a value that indicates whether the Message is disposed.

IsEmpty IsEmpty IsEmpty IsEmpty

返回一个值,该值指示 Message 是否为空。Returns a value that indicates whether the Message is empty.

IsFault IsFault IsFault IsFault

获取一个值,该值指示此消息是否会生成任何 SOAP 错误。Gets a value that indicates whether this message generates any SOAP faults.

Properties Properties Properties Properties

在派生类中重写时,获取消息的处理级别批注集。When overridden in a derived class, gets a set of processing-level annotations to the message.

State State State State

获取此 Message 的当前状态。Gets the current state of this Message.

Version Version Version Version

在派生类中重写时,获取消息的 SOAP 版本。When overridden in a derived class, gets the SOAP version of the message.

方法

BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object) BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object) BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object) BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

开始消息主体内容的异步写入。Starts the asynchronous writing of the contents of the message body.

BeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object) BeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object) BeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object) BeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object)

开始整条消息的异步写入。Starts the asynchronous writing of the entire message.

Close() Close() Close() Close()

关闭 Message 并释放任何资源。Closes the Message and releases any resources.

CreateBufferedCopy(Int32) CreateBufferedCopy(Int32) CreateBufferedCopy(Int32) CreateBufferedCopy(Int32)

将整个 Message 存储到内存缓冲区,以供日后访问。Stores an entire Message into a memory buffer for future access.

CreateMessage(MessageVersion, FaultCode, String, Object, String) CreateMessage(MessageVersion, FaultCode, String, Object, String) CreateMessage(MessageVersion, FaultCode, String, Object, String) CreateMessage(MessageVersion, FaultCode, String, Object, String)

创建一个包含了 SOAP 错误、错误原因和详细信息、版本和操作的消息。Creates a message that contains a SOAP fault, a reason and the detail for the fault, a version and an action.

CreateMessage(MessageVersion, FaultCode, String, String) CreateMessage(MessageVersion, FaultCode, String, String) CreateMessage(MessageVersion, FaultCode, String, String) CreateMessage(MessageVersion, FaultCode, String, String)

创建一个包含了 SOAP 错误、错误原因、版本和操作的消息。Creates a message that contains a SOAP fault, the reason for the fault, a version and an action.

CreateMessage(MessageVersion, MessageFault, String) CreateMessage(MessageVersion, MessageFault, String) CreateMessage(MessageVersion, MessageFault, String) CreateMessage(MessageVersion, MessageFault, String)

创建一个包含了 SOAP 错误、版本和操作的消息。Creates a message that contains a SOAP fault, a version and an action.

CreateMessage(MessageVersion, String) CreateMessage(MessageVersion, String) CreateMessage(MessageVersion, String) CreateMessage(MessageVersion, String)

创建一个包含了版本和操作的消息。Creates a message that contains a version and an action.

CreateMessage(MessageVersion, String, BodyWriter) CreateMessage(MessageVersion, String, BodyWriter) CreateMessage(MessageVersion, String, BodyWriter) CreateMessage(MessageVersion, String, BodyWriter)

创建一个正文包含字节数组的消息。Creates a message with a body that consists of an array of bytes.

CreateMessage(MessageVersion, String, Object) CreateMessage(MessageVersion, String, Object) CreateMessage(MessageVersion, String, Object) CreateMessage(MessageVersion, String, Object)

使用指定的版本、操作和正文创建消息。Creates a message with the specified version, action and body.

CreateMessage(MessageVersion, String, Object, XmlObjectSerializer) CreateMessage(MessageVersion, String, Object, XmlObjectSerializer) CreateMessage(MessageVersion, String, Object, XmlObjectSerializer) CreateMessage(MessageVersion, String, Object, XmlObjectSerializer)

使用指定的版本、操作、消息正文和序列化程序创建消息。Creates a message using the specified version, action, message body and serializer.

CreateMessage(MessageVersion, String, XmlDictionaryReader) CreateMessage(MessageVersion, String, XmlDictionaryReader) CreateMessage(MessageVersion, String, XmlDictionaryReader) CreateMessage(MessageVersion, String, XmlDictionaryReader)

使用指定的版本、操作和正文创建消息。Creates a message with the specified version, action and body.

CreateMessage(MessageVersion, String, XmlReader) CreateMessage(MessageVersion, String, XmlReader) CreateMessage(MessageVersion, String, XmlReader) CreateMessage(MessageVersion, String, XmlReader)

使用指定的读取器、操作和版本创建消息。Creates a message using the specified reader, action and version.

CreateMessage(XmlDictionaryReader, Int32, MessageVersion) CreateMessage(XmlDictionaryReader, Int32, MessageVersion) CreateMessage(XmlDictionaryReader, Int32, MessageVersion) CreateMessage(XmlDictionaryReader, Int32, MessageVersion)

使用指定的读取器、操作和版本创建消息。Creates a message using the specified reader, action and version.

CreateMessage(XmlReader, Int32, MessageVersion) CreateMessage(XmlReader, Int32, MessageVersion) CreateMessage(XmlReader, Int32, MessageVersion) CreateMessage(XmlReader, Int32, MessageVersion)

使用指定的读取器、操作和版本创建消息。Creates a message using the specified reader, action and version.

EndWriteBodyContents(IAsyncResult) EndWriteBodyContents(IAsyncResult) EndWriteBodyContents(IAsyncResult) EndWriteBodyContents(IAsyncResult)

关闭消息主体内容的异步写入。Ends the asynchronous writing of the contents of the message body.

EndWriteMessage(IAsyncResult) EndWriteMessage(IAsyncResult) EndWriteMessage(IAsyncResult) EndWriteMessage(IAsyncResult)

关闭整条消息的异步写入。Ends the asynchronous writing of the entire message.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetBody<T>() GetBody<T>() GetBody<T>() GetBody<T>()

检索此 Message 实例的正文。Retrieves the body of this Message instance.

GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer)

使用指定的序列化程序检索此 Message 的正文。Retrieves the body of this Message using the specified serializer.

GetBodyAttribute(String, String) GetBodyAttribute(String, String) GetBodyAttribute(String, String) GetBodyAttribute(String, String)

检索消息正文的属性。Retrieves the attributes of the message body.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
GetReaderAtBodyContents() GetReaderAtBodyContents() GetReaderAtBodyContents() GetReaderAtBodyContents()

获取访问此消息正文内容的 XML 字典读取器。Gets the XML dictionary reader that accesses the body content of this message.

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object) OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object) OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object) OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

当消息开始写入消息正文的内容时引发事件。Raises an event when the message starts writing the contents of the message body.

OnBeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object) OnBeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object) OnBeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object) OnBeginWriteMessage(XmlDictionaryWriter, AsyncCallback, Object)

引发整个消息写入开始的事件。Raises an event the writing of entire messages starts.

OnBodyToString(XmlDictionaryWriter) OnBodyToString(XmlDictionaryWriter) OnBodyToString(XmlDictionaryWriter) OnBodyToString(XmlDictionaryWriter)

消息正文转换成字符串时调用。Called when the message body is converted to a string.

OnClose() OnClose() OnClose() OnClose()

在关闭消息时调用。Called when the message is closing.

OnCreateBufferedCopy(Int32) OnCreateBufferedCopy(Int32) OnCreateBufferedCopy(Int32) OnCreateBufferedCopy(Int32)

创建消息缓冲区来存储此消息时调用。Called when a message buffer is created to store this message.

OnEndWriteBodyContents(IAsyncResult) OnEndWriteBodyContents(IAsyncResult) OnEndWriteBodyContents(IAsyncResult) OnEndWriteBodyContents(IAsyncResult)

当写入消息正文的内容结束时引发事件。Raises an event when writing of the contents of the message body ends.

OnEndWriteMessage(IAsyncResult) OnEndWriteMessage(IAsyncResult) OnEndWriteMessage(IAsyncResult) OnEndWriteMessage(IAsyncResult)

当整个消息的写入结束时引发事件。Raises an event when the writing of the entire message ends.

OnGetBody<T>(XmlDictionaryReader) OnGetBody<T>(XmlDictionaryReader) OnGetBody<T>(XmlDictionaryReader) OnGetBody<T>(XmlDictionaryReader)

检索消息正文时调用。Called when the body of the message is retrieved.

OnGetBodyAttribute(String, String) OnGetBodyAttribute(String, String) OnGetBodyAttribute(String, String) OnGetBodyAttribute(String, String)

检索消息正文的属性时调用。Called when the attributes of the message body is retrieved.

OnGetReaderAtBodyContents() OnGetReaderAtBodyContents() OnGetReaderAtBodyContents() OnGetReaderAtBodyContents()

当一个访问这条消息的征文内容的 XML 字典读取器被检索时,调用。 Called when an XML dictionary reader that accesses the body content of this message is retrieved.

OnWriteBodyContents(XmlDictionaryWriter) OnWriteBodyContents(XmlDictionaryWriter) OnWriteBodyContents(XmlDictionaryWriter) OnWriteBodyContents(XmlDictionaryWriter)

将消息正文写入 XML 文件时调用。Called when the message body is written to an XML file.

OnWriteMessage(XmlDictionaryWriter) OnWriteMessage(XmlDictionaryWriter) OnWriteMessage(XmlDictionaryWriter) OnWriteMessage(XmlDictionaryWriter)

将整个消息写入 XML 文件时调用。Called when the entire message is written to an XML file.

OnWriteStartBody(XmlDictionaryWriter) OnWriteStartBody(XmlDictionaryWriter) OnWriteStartBody(XmlDictionaryWriter) OnWriteStartBody(XmlDictionaryWriter)

将开始正文写入 XML 文件时调用。Called when the start body is written to an XML file.

OnWriteStartEnvelope(XmlDictionaryWriter) OnWriteStartEnvelope(XmlDictionaryWriter) OnWriteStartEnvelope(XmlDictionaryWriter) OnWriteStartEnvelope(XmlDictionaryWriter)

将开始信封写入 XML 文件时调用。Called when the start envelope is written to an XML file.

OnWriteStartHeaders(XmlDictionaryWriter) OnWriteStartHeaders(XmlDictionaryWriter) OnWriteStartHeaders(XmlDictionaryWriter) OnWriteStartHeaders(XmlDictionaryWriter)

将开始标头写入 XML 文件时调用。Called when the start header is written to an XML file.

ToString() ToString() ToString() ToString()

返回表示当前 Message 实例的字符串。Returns a string that represents the current Message instance.

WriteBody(XmlDictionaryWriter) WriteBody(XmlDictionaryWriter) WriteBody(XmlDictionaryWriter) WriteBody(XmlDictionaryWriter)

使用指定的 XmlDictionaryWriter 写入正文元素。Writes the body element using the specified XmlDictionaryWriter.

WriteBody(XmlWriter) WriteBody(XmlWriter) WriteBody(XmlWriter) WriteBody(XmlWriter)

使用指定的 XmlWriter 序列化消息正文。Serializes the message body using the specified XmlWriter.

WriteBodyContents(XmlDictionaryWriter) WriteBodyContents(XmlDictionaryWriter) WriteBodyContents(XmlDictionaryWriter) WriteBodyContents(XmlDictionaryWriter)

使用指定的 XmlDictionaryWriter 序列化正文内容。Serializes the body content using the specified XmlDictionaryWriter.

WriteMessage(XmlDictionaryWriter) WriteMessage(XmlDictionaryWriter) WriteMessage(XmlDictionaryWriter) WriteMessage(XmlDictionaryWriter)

使用指定的 XmlDictionaryWriter 序列化整个消息。Serializes the entire message using the specified XmlDictionaryWriter.

WriteMessage(XmlWriter) WriteMessage(XmlWriter) WriteMessage(XmlWriter) WriteMessage(XmlWriter)

使用指定的 XmlWriter 序列化整个消息。Serializes the entire message using the specified XmlWriter.

WriteStartBody(XmlDictionaryWriter) WriteStartBody(XmlDictionaryWriter) WriteStartBody(XmlDictionaryWriter) WriteStartBody(XmlDictionaryWriter)

使用指定的 XmlDictionaryWriter 序列化消息的起始正文。Serializes the start body of the message using the specified XmlDictionaryWriter.

WriteStartBody(XmlWriter) WriteStartBody(XmlWriter) WriteStartBody(XmlWriter) WriteStartBody(XmlWriter)

使用指定的 XmlDictionaryWriter 序列化消息的起始正文。Serializes the start body of the message using the specified XmlDictionaryWriter.

WriteStartEnvelope(XmlDictionaryWriter) WriteStartEnvelope(XmlDictionaryWriter) WriteStartEnvelope(XmlDictionaryWriter) WriteStartEnvelope(XmlDictionaryWriter)

使用指定的 XmlDictionaryWriter 序列化开始信封。Serializes the start envelope using the specified XmlDictionaryWriter.

显式界面实现

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

关闭此消息使用的所有资源。Closes all the resources used by this message. 不能继承此方法。This method cannot be inherited.

Extension Methods

ToHttpRequestMessage(Message) ToHttpRequestMessage(Message) ToHttpRequestMessage(Message) ToHttpRequestMessage(Message)

通过 HttpRequestMessage 实例创建 Message 实例。Creates a HttpRequestMessage instance from an Message instance.

ToHttpResponseMessage(Message) ToHttpResponseMessage(Message) ToHttpResponseMessage(Message) ToHttpResponseMessage(Message)

通过 HttpResponseMessage 实例创建 Message 实例。Creates a HttpResponseMessage instance from an Message instance.

适用于