MessageEncoder.WriteMessage 方法

定义

在派生类中重写时,将消息写入指定的流或缓冲区。

重载

WriteMessage(Message, Stream)

在派生类中重写时,将消息写入指定的流中。

WriteMessage(Message, Int32, BufferManager)

将小于指定大小的消息写入字节数组缓冲区。

WriteMessage(Message, Int32, BufferManager, Int32)

在派生类中重写时,将小于指定大小的消息按指定的偏移量写入字节数组缓冲区。

WriteMessage(Message, Stream)

Source:
MessageEncoder.cs
Source:
MessageEncoder.cs
Source:
MessageEncoder.cs

在派生类中重写时,将消息写入指定的流中。

public:
 abstract void WriteMessage(System::ServiceModel::Channels::Message ^ message, System::IO::Stream ^ stream);
public abstract void WriteMessage (System.ServiceModel.Channels.Message message, System.IO.Stream stream);
abstract member WriteMessage : System.ServiceModel.Channels.Message * System.IO.Stream -> unit
Public MustOverride Sub WriteMessage (message As Message, stream As Stream)

参数

message
Message

要写入 Messagestream

stream
Stream

Stream 要写入的 message 对象。

示例

下面的代码演示如何实现 WriteMessage(Message, Stream) 方法。

public override void WriteMessage(Message message, Stream stream)
{
    XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
    message.WriteMessage(writer);
    writer.Close();
}

适用于

WriteMessage(Message, Int32, BufferManager)

Source:
MessageEncoder.cs
Source:
MessageEncoder.cs
Source:
MessageEncoder.cs

将小于指定大小的消息写入字节数组缓冲区。

public:
 ArraySegment<System::Byte> WriteMessage(System::ServiceModel::Channels::Message ^ message, int maxMessageSize, System::ServiceModel::Channels::BufferManager ^ bufferManager);
public ArraySegment<byte> WriteMessage (System.ServiceModel.Channels.Message message, int maxMessageSize, System.ServiceModel.Channels.BufferManager bufferManager);
member this.WriteMessage : System.ServiceModel.Channels.Message * int * System.ServiceModel.Channels.BufferManager -> ArraySegment<byte>
Public Function WriteMessage (message As Message, maxMessageSize As Integer, bufferManager As BufferManager) As ArraySegment(Of Byte)

参数

message
Message

要写入消息缓冲区的 Message

maxMessageSize
Int32

可写入的最大消息大小。

bufferManager
BufferManager

BufferManager,管理将消息写入到的缓冲区。

返回

一个类型为 ArraySegment<T>byte,它提供将消息序列化到的缓冲区。

注解

此方法调用抽象方法 WriteMessage(Message, Int32, BufferManager, Int32),其中 messageOffset = 0。

适用于

WriteMessage(Message, Int32, BufferManager, Int32)

Source:
MessageEncoder.cs
Source:
MessageEncoder.cs
Source:
MessageEncoder.cs

在派生类中重写时,将小于指定大小的消息按指定的偏移量写入字节数组缓冲区。

public:
 abstract ArraySegment<System::Byte> WriteMessage(System::ServiceModel::Channels::Message ^ message, int maxMessageSize, System::ServiceModel::Channels::BufferManager ^ bufferManager, int messageOffset);
public abstract ArraySegment<byte> WriteMessage (System.ServiceModel.Channels.Message message, int maxMessageSize, System.ServiceModel.Channels.BufferManager bufferManager, int messageOffset);
abstract member WriteMessage : System.ServiceModel.Channels.Message * int * System.ServiceModel.Channels.BufferManager * int -> ArraySegment<byte>
Public MustOverride Function WriteMessage (message As Message, maxMessageSize As Integer, bufferManager As BufferManager, messageOffset As Integer) As ArraySegment(Of Byte)

参数

message
Message

要写入消息缓冲区的 Message

maxMessageSize
Int32

可写入的最大消息大小。

bufferManager
BufferManager

BufferManager,管理将消息写入到的缓冲区。

messageOffset
Int32

从提供缓冲区的字节数组开头开始的段的偏移量。

返回

一个类型为 ArraySegment<T>byte,它提供将消息序列化到的缓冲区。

示例

下面的代码演示如何实现 WriteMessage(Message, Int32, BufferManager, Int32) 方法。

public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
    MemoryStream stream = new MemoryStream();
    XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
    message.WriteMessage(writer);
    writer.Close();

    byte[] messageBytes = stream.GetBuffer();
    int messageLength = (int)stream.Position;
    stream.Close();

    int totalLength = messageLength + messageOffset;
    byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
    Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);

    ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);
    return byteArray;
}

注解

此方法由 WriteMessage(Message, Int32, BufferManager, Int32) 调用。

适用于