MessageEncoder.WriteMessage Method (Message, Int32, BufferManager, Int32)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.ServiceModel.Channels
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public abstract ArraySegment<byte> WriteMessage(
	Message message,
	int maxMessageSize,
	BufferManager bufferManager,
	int messageOffset
)

Parameters

message
Type: System.ServiceModel.Channels.Message

The Message to write to the message buffer.

maxMessageSize
Type: System.Int32

The maximum message size that can be written.

bufferManager
Type: System.ServiceModel.Channels.BufferManager

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

messageOffset
Type: System.Int32

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

Return Value

Type: System.ArraySegment<Byte>

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

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

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: