BodyWriter Classe

Definizione

Rappresenta il writer del corpo del messaggio.

public ref class BodyWriter abstract
public abstract class BodyWriter
type BodyWriter = class
Public MustInherit Class BodyWriter
Ereditarietà
BodyWriter
Derivato

Esempio

Nell'esempio seguente viene illustrato come derivare una classe da BodyWriter. Questa override accetta una matrice di stringhe e le scrive in un oggetto XmlDictionaryWriter.

using System;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Xml;

namespace UEBodyWriter
{
    class MyBodyWriter : BodyWriter
    {
        const string textTag = "text";
        string[] bodySegment;

        public MyBodyWriter(string[] strData) : base(true)
        {
            int length = strData.Length;

            this.bodySegment = new string[length];
            for (int i = 0; i < length; i++)
            {
                this.bodySegment[i] = strData[i];
            }
        }

        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
           writer.WriteStartElement(textTag);

           foreach (string str in bodySegment)
           {
               writer.WriteString(str);
           }

            writer.WriteEndElement();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string[] strings = {"Hello", "world"};
            MyBodyWriter bw = new MyBodyWriter(strings);

            StringBuilder strBuilder = new StringBuilder(10);
            XmlWriter writer = XmlWriter.Create(strBuilder);
            XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);

            bw.WriteBodyContents(dictionaryWriter);
            dictionaryWriter.Flush();
        }
    }
}


Imports System.Text
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Xml

Namespace UEBodyWriter
    Friend Class MyBodyWriter
        Inherits BodyWriter
        Private Const textTag As String = "text"
        Private bodySegment() As String

        Public Sub New(ByVal strData() As String)
            MyBase.New(True)
            Dim length = strData.Length

            Me.bodySegment = New String(length - 1){}
            For i = 0 To length - 1
                Me.bodySegment(i) = strData(i)
            Next i
        End Sub

        Protected Overrides Sub OnWriteBodyContents(ByVal writer As XmlDictionaryWriter)
           writer.WriteStartElement(textTag)

            For Each str As String In bodySegment
                writer.WriteString(str)
            Next str

            writer.WriteEndElement()
        End Sub
    End Class

    Module Module1
        Sub Main(ByVal args() As String)
            Dim strings() As String = {"Hello", "world"}
            Dim bw As New MyBodyWriter(strings)

            Dim strBuilder As New StringBuilder(10)
            Dim writer = XmlWriter.Create(strBuilder)
            Dim dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer)

            bw.WriteBodyContents(dictionaryWriter)
            dictionaryWriter.Flush()
        End Sub
    End Module
End Namespace

Commenti

Un messaggio è costituito da intestazioni e un corpo. Le intestazioni vengono memorizzate nel buffer e il corpo viene trasmesso. Poiché il corpo viene trasmesso, l'utente non può passare il contenuto effettivo del corpo a un messaggio. L'utente deve invece passare una classe che sa scrivere il corpo quando viene richiesto di farlo. A tale scopo, passare una classe derivata da BodyWriter a Message. Un messaggio chiama la classe derivata da BodyWriter ogni volta che richiede che il corpo venga scritto usando un oggetto XmlWriter.

Costruttori

Nome Descrizione
BodyWriter(Boolean)

Inizializza una nuova istanza della BodyWriter classe che indica in modo esplicito se memorizzare nel buffer.

Proprietà

Nome Descrizione
IsBuffered

Ottiene un valore che indica se il metodo di scrittura può essere chiamato più volte.

Metodi

Nome Descrizione
BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

Inizia a scrivere il contenuto del corpo per il writer del corpo con writer, callback e stato specificati.

CreateBufferedCopy(Int32)

Crea una copia memorizzata nel buffer del corpo.

EndWriteBodyContents(IAsyncResult)

Termina la scrittura del contenuto del corpo.

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetType()

Ottiene il Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale del Objectcorrente.

(Ereditato da Object)
OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

Genera un evento quando il writer del corpo inizia a scrivere il contenuto del corpo con writer, callback e stato specificati.

OnCreateBufferedCopy(Int32)

Fornisce un punto di estendibilità quando il contenuto del corpo viene scritto.

OnEndWriteBodyContents(IAsyncResult)

Genera un evento quando il writer del corpo termina la scrittura del contenuto del corpo.

OnWriteBodyContents(XmlDictionaryWriter)

Se implementato, fornisce un punto di estendibilità quando il contenuto del corpo viene scritto.

ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)
WriteBodyContents(XmlDictionaryWriter)

Scrive il contenuto del corpo del messaggio.

Si applica a