BodyWriter 类

定义

表示消息正文的编写器。

public ref class BodyWriter abstract
public abstract class BodyWriter
type BodyWriter = class
Public MustInherit Class BodyWriter
继承
BodyWriter
派生

示例

下面的示例演示如何从 BodyWriter 派生类。 此重写接收一个字符串数组,然后将其写入 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

注解

消息由标头和正文组成。 标头将进行缓冲,正文进行流处理。 由于正文进行流处理,用户无法将正文的实际内容传递到消息。 当要求用户传递内容时,用户必须传递一个知道如何写入正文的类。 此工作通过将一个派生自 BodyWriter 的类传递到 Message 来实现。 每当要求使用 BodyWriter 写入正文时,消息都调用派生自 XmlWriter 的类。

构造函数

BodyWriter(Boolean)

初始化 BodyWriter 类的新实例,该实例显式指示是否缓冲。

属性

IsBuffered

获取一个值,该值指示是否可以多次调用写入方法。

方法

BeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

当正文写入器开始使用指定的写入器、回调和状态时引发事件。

CreateBufferedCopy(Int32)

创建正文的缓冲副本。

EndWriteBodyContents(IAsyncResult)

关闭主体内容的写入。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnBeginWriteBodyContents(XmlDictionaryWriter, AsyncCallback, Object)

当正文写入器开始使用指定的写入器、回调和状态时引发事件。

OnCreateBufferedCopy(Int32)

在写入正文内容时提供可扩展点。

OnEndWriteBodyContents(IAsyncResult)

当正文写入器终止写入正文时引发事件。

OnWriteBodyContents(XmlDictionaryWriter)

实现时,提供写入正文内容时的可扩展点。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
WriteBodyContents(XmlDictionaryWriter)

写出消息正文的内容。

适用于