MessageBuffer.WriteMessage Method (Stream)

 

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
)

Parameters

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 
....message.WriteMessage(writer);
....//Recreate the message 
....message = buffer.CreateMessage();
....//Flush the contents of the writer so that the stream gets updated
....writer.Flush();
....stream.Flush();
....//Convert the stream to an array
....byte[] retval = stream.ToArray();
    return retval;
}

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: