BodyWriter Class

 

Represents the writer of the message body.

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


public abstract class BodyWriter

NameDescription
System_CAPS_protmethodBodyWriter(Boolean)

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

NameDescription
System_CAPS_pubpropertyIsBuffered

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

NameDescription
System_CAPS_pubmethodBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

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

System_CAPS_pubmethodCreateBufferedCopy(Int32)

Creates a buffered copy of the body.

System_CAPS_pubmethodEndWriteBodyContents(IAsyncResult)

Ends the writing of body contents.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

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

System_CAPS_protmethodOnCreateBufferedCopy(Int32)

Provides an extensibility point when the body contents are written.

System_CAPS_protmethodOnEndWriteBodyContents(IAsyncResult)

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

System_CAPS_protmethodOnWriteBodyContents(XmlDictionaryWriter)

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

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodWriteBodyContents(XmlDictionaryWriter)

Writes out the contents of the message body.

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.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: