MessageBuffer.Close Metodo

Definizione

Termina l'uso del buffer.

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

Esempio

Nell'esempio seguente viene illustrato come chiudere correttamente un buffer di messaggi.

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

Commenti

È consigliabile chiudere sempre un'istanza MessageBuffer chiamando Close al termine dell'utilizzo. In questo modo, le risorse di sistema possono essere liberate prima.

Se è stato chiamato CreateBufferedCopy per creare un buffer di messaggi di un messaggio ed è stato esaminato il messaggio usando CreateMessage, si otterrà un InvalidOperationException oggetto quando si tenta di chiudere il buffer usando questo metodo. Per evitare questo problema, è necessario ricreare il messaggio dal buffer prima della chiusura. Vedere l'esempio di codice nella sezione Esempio per una dimostrazione dello scenario precedente e un modo per risolvere il problema.

Si applica a