Uređivanje

Dijeli putem


BodyWriter Class

Definition

Represents the writer of the message body.

public ref class BodyWriter abstract
public abstract class BodyWriter
type BodyWriter = class
Public MustInherit Class BodyWriter
Inheritance
BodyWriter
Derived

Examples

The following example shows how to derive a class from BodyWriter. This override takes in an array of strings and writes them to a 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

Remarks

A message consists of headers and a body. The headers are buffered and the body is streamed. Because the body is streamed, the user cannot pass the actual content of the body to a message. Instead the user must pass a class that knows how to write the body when asked to do so. This is done by passing a class derived from BodyWriter to the Message. A message calls the class derived from BodyWriter whenever it requires the body to be written using an XmlWriter.

Constructors

BodyWriter(Boolean)

Initializes a new instance of the BodyWriter class that explicitly indicates whether to buffer.

Properties

IsBuffered

Gets a value that indicates whether the write method can be called multiple times.

Methods

BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

Starts to write body contents for the body writer with specified writer, callback and state.

CreateBufferedCopy(Int32)

Creates a buffered copy of the body.

EndWriteBodyContents(IAsyncResult)

Ends the writing of body contents.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

Raises an event when the body writer starts to write body contents with specified writer, callback and state.

OnCreateBufferedCopy(Int32)

Provides an extensibility point when the body contents are written.

OnEndWriteBodyContents(IAsyncResult)

Raises an event when the body writer ends writing body contents.

OnWriteBodyContents(XmlDictionaryWriter)

When implemented, provides an extensibility point when the body contents are written.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
WriteBodyContents(XmlDictionaryWriter)

Writes out the contents of the message body.

Applies to