This documentation is archived and is not being maintained.

MessageBuffer.WriteMessage Method

Writes the entire content of this buffer to the specified IO stream.

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

public virtual void WriteMessage(
	Stream stream


Type: System.IO.Stream

An IO stream that the entire content of this buffer is written to.

This function uses a binary encoder instead of a UTF-8 encoder. Therefore, you cannot directly convert from a MessageBuffer to a Message. The code in the example section shows how to work around this problem.

private byte[] ConvertMessageToByteArray(ref Message message) 
....//Memory stream that contains the message
....MemoryStream stream = new MemoryStream();
....//Create an XmlWriter to serialize the message into a byte array
....XmlWriterSettings settings = new XmlWriterSettings();
....settings.Encoding = System.Text.Encoding.UTF8;
....XmlWriter writer = XmlWriter.Create(stream, settings);
....//Copy the message into a buffer 
....//Note: This call changes the original message's state
....MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue);
....//Create a copy of the message
....message = buffer.CreateMessage();
....//Serialize the message to the XmlWriter 
....//Recreate the message 
....message = buffer.CreateMessage();
....//Flush the contents of the writer so that the stream gets updated
....//Convert the stream to an array
....byte[] retval = stream.ToArray();
    return retval;

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

.NET Compact Framework

Supported in: 3.5