MessageBuffer MessageBuffer MessageBuffer MessageBuffer Class


表示一个内存缓冲区,该内存缓冲区存储了未来要使用的整个消息。Represents a memory buffer that stores an entire message for future consumption.

public ref class MessageBuffer abstract : IDisposable
public abstract class MessageBuffer : IDisposable
type MessageBuffer = class
    interface IDisposable
Public MustInherit Class MessageBuffer
Implements IDisposable


Message 实例的正文只可使用或写入一次。The body of a Message instance can only be consumed or written once. 如果希望不止一次使用 Message 实例,则应该使用 MessageBuffer 类将整个 Message 实例全部存储到内存。If you wish to consume a Message instance more than once, you should use the MessageBuffer class to completely store an entire Message instance into memory.

MessageBuffer 实例是通过调用 CreateBufferedCopy 实例的 Message 构造的。A MessageBuffer instance is constructed by calling CreateBufferedCopy of a Message instance. 然后会创建并返回一个新的 MessageBuffer,它将获得对 Message 的所有权,并将整个内容读取到内存。A new MessageBuffer is then created and returned, which assumes ownership of the Message and reads the entire content into memory.

为了从 Message 检索 MessageBuffer 的副本,必须调用 CreateMessageMessageBuffer 方法。In order to retrieve a copy of a Message from the MessageBuffer, you must call the CreateMessage method of the MessageBuffer. 这会返回您所提供的原始 Message 实例的相同副本。This returns an identical copy of the original Message instance you provided.

可以通过将 BufferSize 设置为所需的最大字节数来控制缓冲区的最大大小。You can control the maximum size of the buffer by setting BufferSize to the maximum number of bytes desired. 这个最大数无需包含任何与生成缓冲区相关的瞬态分配或附加到消息的属性。This number does not necessarily cover any transient allocations related to building the buffer, or properties attached to the message.

每次使用完 MessageBuffer 实例后,您都应该调用 Close 以关闭该实例。You should always close a MessageBuffer instance by calling Close when finished working with it. 这可能会允许更快地释放系统资源。This allows system resources to potentially be freed sooner.

针对从此类派生的托管 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, you should suppress the build warning and call SuppressFinalize(Object) and the finalizer itself from (On)(Begin)Close (and/or OnAbort) in order to emulate what would have been the auto-generated IDisposable behavior.


MessageBuffer() MessageBuffer() MessageBuffer() MessageBuffer()

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


BufferSize BufferSize BufferSize BufferSize

获取此 MessageBuffer 使用的近似字节数。Gets the approximate number of bytes consumed by this MessageBuffer.

MessageContentType MessageContentType MessageContentType MessageContentType

获取存储在此缓冲区中的消息内容的类型。Gets the type of message content stored in this buffer.


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

缓冲区使用完毕。Finishes working with the buffer.

CreateMessage() CreateMessage() CreateMessage() CreateMessage()

返回原始消息的副本。Returns a copy of the original message.

CreateNavigator() CreateNavigator() CreateNavigator() CreateNavigator()

创建一个用于定位此对象的新 XPathNavigator 对象。Creates a new XPathNavigator object for navigating this object. 不能继承此方法。This method cannot be inherited.

CreateNavigator(Int32) CreateNavigator(Int32) CreateNavigator(Int32) CreateNavigator(Int32)

使用位于指定节点上的导航器,创建一个用于定位此对象的新 XPathNavigator 对象。Creates a new XPathNavigator object for navigating this object, with the navigator positioned on the node specified.

CreateNavigator(Int32, XmlSpace) CreateNavigator(Int32, XmlSpace) CreateNavigator(Int32, XmlSpace) CreateNavigator(Int32, XmlSpace)

使用位于指定节点和 xml:space 范围的导航器,创建一个用于定位此对象的新 XPathNavigator 对象。Creates a new XPathNavigator object for navigating this object, with the navigator positioned on the specified node and xml:space scope.

CreateNavigator(XmlSpace) CreateNavigator(XmlSpace) CreateNavigator(XmlSpace) CreateNavigator(XmlSpace)

在指定的 XPathNavigator 范围,创建一个用于定位此对象的新 xml:space 对象。Creates a new XPathNavigator object for navigating this object, with the specified xml:space scope.

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

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

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

(Inherited from Object)
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)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
WriteMessage(Stream) WriteMessage(Stream) WriteMessage(Stream) WriteMessage(Stream)

将此缓冲区中的整个内容写入指定的 IO 流。Writes the entire content of this buffer to the specified IO stream.


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

释放由 MessageBuffer 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the MessageBuffer and optionally releases the managed resources. 不能继承此方法。This method cannot be inherited.