IStreamProvider 接口

定义

表示一个可以由提供流的类来实现的接口。Represents an interface that can be implemented by classes providing streams.

public interface class IStreamProvider
public interface IStreamProvider
type IStreamProvider = interface
Public Interface IStreamProvider

注解

将包含经过流处理的正文的传出消息写入 XML 编写器时,Message 将在其 OnWriteBodyContents(XmlDictionaryWriter) 实现中使用类似于下面的一系列调用:When writing an outgoing message containing a streamed body to an XML writer, the Message will use a sequence of calls similar to the following in its OnWriteBodyContents(XmlDictionaryWriter) implementation:

  • 写入流前面的任何必要信息(例如 XML 开始标记)。Write any necessary information preceding the stream (For example, the opening XML tag).

  • 写入流。Write the stream.

  • 写入流后面的任何信息(例如 XML 结束标记)。Write any information following the stream (For example, the closing XML tag).

这对于类似于文本 XML 编码的编码可以正常工作。This works well with encodings that are similar to the textual XML encoding. 但是,有一些编码不将 XML 信息集信息(例如,开始和结束 XML 元素的标记)和元素中包含的数据放在一起。However, there are some encodings that do not place XML infoset information (For example, tags for starting and ending XML elements) together with the data contained within elements. 例如,在 MTOM 编码中,消息拆分为多个部分。For example, in the MTOM encoding, the message is split into multiple parts. 一部分包含 XML 信息集,其中可能包含对实际元素内容的其他部分的引用。One part contains the XML infoset, which may contain references to other parts for actual element contents. 由于 XML 信息集通常比流处理的内容小,因此有必要缓冲信息集,将其写出,然后以流处理方式写入内容。Since the XML infoset will normally be small compared to the streamed contents, it makes sense to buffer the infoset, write it out, and then write the contents in a streamed way. 这意味着在写入结束元素标记时,应当尚未写出流。This means that by the time the closing element tag is written, we should not have written out the stream yet.

为此,应使用 IStreamProvider 接口。For this purpose, the IStreamProvider interface is used. 该接口具有一个可返回要写入的流的 GetStream() 方法。The interface has a GetStream() method that returns the stream to be written. 写出 OnWriteBodyContents(XmlDictionaryWriter) 中经过流处理的消息正文的正确方式如下:The correct way to write out a streamed message body in OnWriteBodyContents(XmlDictionaryWriter) is as follows:

  • 写入流前面的任何必要信息(例如 XML 开始标记)Write any necessary information preceding the stream (For example, the opening XML tag)

  • 对采用 WriteValue 、具有 XmlDictionaryWriter 实现(该实现可返回要写入的流)的 IStreamProvider调用 IStreamProvider 重载。Call the WriteValue overload on the XmlDictionaryWriter that takes an IStreamProvider, with an IStreamProvider implementation that returns the stream to be written.

  • 写入流后面的任何信息(例如 XML 结束标记)Write any information following the stream (For example, the closing XML tag)

使用此方法时,XML 编写器可以选择何时调用 GetStream() 和写出经过流处理的数据。With this approach, the XML writer has a choice of when to call GetStream() and write out the streamed data. 例如,文本和二进制 XML 编写器将立即调用此方法并写出开始标记和结束标记之间的经过流处理的内容。For example, the textual and binary XML writers will call it immediately and write out the streamed contents in between the start and end tags. MTOM 编写器准备写入消息的相应部分时,它可以决定以后调用 GetStream()The MTOM writer may decide to call GetStream() later, when it is ready to write the appropriate part of the message.

方法

GetStream()

获取流。Gets a stream.

ReleaseStream(Stream)

将流释放到输出。Releases a stream to output.

适用于