Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

BodyWriter Class

Represents the writer of the message body.

System.Object
  System.ServiceModel.Channels.BodyWriter
    System.ServiceModel.Channels.StreamBodyWriter

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

'Declaration
Public MustInherit Class BodyWriter

The BodyWriter type exposes the following members.

  NameDescription
Protected methodSupported by Portable Class LibraryBodyWriterInitializes a new instance of the BodyWriter class that explicitly indicates whether to buffer.
Top

  NameDescription
Public propertySupported by Portable Class LibraryIsBufferedGets a value that indicates whether the write method can be called multiple times.
Top

  NameDescription
Public methodCreateBufferedCopyCreates a buffered copy of the body.
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnCreateBufferedCopyProvides an extensibility point when the body contents are written.
Protected methodSupported by Portable Class LibraryOnWriteBodyContentsWhen implemented, provides an extensibility point when the body contents are written.
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryWriteBodyContentsWrites out the contents of the message body.
Top

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.




Imports System
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


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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

Community Additions

Show:
© 2017 Microsoft