MessageBuffer.Close 方法

定义

缓冲区使用完毕。Finishes working with the buffer.

public:
 abstract void Close();
public abstract void Close ();
abstract member Close : unit -> unit
Public MustOverride Sub Close ()

示例

下面的示例演示如何正确关闭消息缓冲区。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();
    var info = new StringBuilder();
    XmlWriter writer = XmlWriter.Create(info);  
    writer.WriteNode(reader, true);  
    writer.Close();  
    // Resolution:  Re-create the message from the buffer before  
    // closing.  
    reply = buffer.CreateMessage();  
    // You can close the buffer after the message has been recreated.  
    buffer.Close();
}

注解

每次使用完 MessageBuffer 实例后,您都应该调用 Close 以关闭该实例。You should always close a MessageBuffer instance by calling Close when finished working with it. 这可能会允许更快地释放系统资源。This allows system resources to potentially be freed sooner.

当尝试用此方法关闭缓冲区时,如果已调用 CreateBufferedCopy 创建了消息的消息缓冲区,并使用 CreateMessage 检查了消息,则会收到 InvalidOperationExceptionIf 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.

适用于