This documentation is archived and is not being maintained.

MessageBuffer.Close Method

Finishes working with the buffer.

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

public abstract void Close()

You should always close a MessageBuffer instance by calling Close when finished working with it. This ensures the underlying Message (the message passed into the constructor) is also closed, freeing system resources.

If you have called CreateBufferedCopy to create a message buffer of a message, and inspected the message using CreateMessage, you will get a InvalidOperationException when you attempt to close the buffer using this method. To avoid this problem, you need to recreate the message from the buffer before closing. See the code sample in the Example section for a demonstration of the previous scenario and a way to resolve this problem.

The following example demonstrates how to properly close a message buffer.

public void AfterReceiveReply(ref Message reply, object correlationState)
    // Create the buffer
    MessageBuffer buffer = reply.CreateBufferedCopy(13000);
    // Inspect the response (for example, extract the body contents)
    Message thisReply = buffer.CreateMessage();
    XmlDictionaryReader reader = thisReply.GetReaderAtBodyContents();
    StringBuilder info = new StringBuilder();
    XmlWriter writer = XmlWriter.Create(info);
    writer.WriteNode(reader, true);
    // Resolution:  Re-create the message from the buffer before
    // closing.
    reply = buffer.CreateMessage();
    // You can close the buffer after the message has been recreated.


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