MessageEncoder.WriteMessage Method

Definition

When overridden in a derived class, writes a message to a specified stream or buffer.

Overloads

WriteMessage(Message, Stream)

When overridden in a derived class, writes a message to a specified stream.

WriteMessage(Message, Int32, BufferManager)

Writes a message less than a specified size to a byte array buffer.

WriteMessage(Message, Int32, BufferManager, Int32)

When overridden in a derived class, writes a message of less than a specified size to a byte array buffer at the specified offset.

WriteMessage(Message, Stream)

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

When overridden in a derived class, writes a message to a specified stream.

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)

Parameters

message
Message

The Message to write to the stream.

stream
Stream

The Stream object to which the message is written.

Examples

The following code shows how to implement the WriteMessage(Message, Stream) method.

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

Applies to

WriteMessage(Message, Int32, BufferManager)

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

Writes a message less than a specified size to a byte array buffer.

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)

Parameters

message
Message

The Message to write to the message buffer.

maxMessageSize
Int32

The maximum message size that can be written.

bufferManager
BufferManager

The BufferManager that manages the buffer to which the message is written.

Returns

A ArraySegment<T> of type byte that provides the buffer to which the message is serialized.

Remarks

This method calls the abstract method WriteMessage(Message, Int32, BufferManager, Int32) with messageOffset = 0.

Applies to

WriteMessage(Message, Int32, BufferManager, Int32)

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

When overridden in a derived class, writes a message of less than a specified size to a byte array buffer at the specified offset.

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)

Parameters

message
Message

The Message to write to the message buffer.

maxMessageSize
Int32

The maximum message size that can be written.

bufferManager
BufferManager

The BufferManager that manages the buffer to which the message is written.

messageOffset
Int32

The offset of the segment that begins from the start of the byte array that provides the buffer.

Returns

A ArraySegment<T> of type byte that provides the buffer to which the message is serialized.

Examples

The following code shows how to implement the WriteMessage(Message, Int32, BufferManager, Int32) method.

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;
}

Remarks

This method is called by WriteMessage(Message, Int32, BufferManager, Int32).

Applies to