TransferMode 枚举


指示通道是使用流处理模式还是缓冲模式来传输请求和响应消息。Indicates whether a channel uses streamed or buffered modes for the transfer of request and response messages.

public enum class TransferMode
public enum TransferMode
type TransferMode = 
Public Enum TransferMode


Buffered 0

对请求和响应消息进行缓冲处理。The request and response messages are both buffered.

Streamed 1

对请求和响应消息进行流式处理。The request and response messages are both streamed.

StreamedRequest 2

对请求消息进行流式处理,对响应消息进行缓冲处理。The request message is streamed and the response message is buffered.

StreamedResponse 3

对请求消息进行缓冲处理,对响应消息进行流式处理。The request message is buffered and the response message is streamed.


下面的示例通过代码将TcpTransportBindingElement. transfermode.buffered属性设置为 StreamedThe following example sets the TcpTransportBindingElement.TransferMode property to Streamed through code:

TcpTransportBindingElement transport = new TcpTransportBindingElement();  
transport.TransferMode = TransferMode.Streamed;  
BinaryMessageEncodingBindingElement encoder = new BinaryMessageEncodingBindingElement();  
CustomBinding binding = new CustomBinding(encoder, transport);  

下面的示例将TcpTransportBindingElement属性设置为通过配置 StreamedThe following example sets the TcpTransportBindingElement.TransferMode property to Streamed through configuration:

    <binding name="streamingBinding">  
        <binaryMessageEncoding />  
            <tcpTransport transferMode="Streamed" />  


Windows Communication Foundation (WCF)传输支持在每个方向传输消息两种模式:Windows Communication Foundation (WCF) transports support two modes of transferring messages in each direction:

  • 缓冲传输将整个消息保留在内存缓冲区中,直到传输完成。Buffered transfers hold the entire message in a memory buffer until the transfer is complete.

  • 流传输仅会对消息头进行缓冲,并以流形式公开消息正文,也就是,一次只能读取一小部分消息。Streamed transfers only buffer the message headers and expose the message body as a stream, from which smaller portions can be read at a time.

将传输模式设置为 Streamed 将在两个方向上启用流通信。Setting the transfer mode to Streamed enables streaming communication in both directions. 将传输模式设置为 StreamedRequestStreamedResponse 将仅在指示的方向上启用流通信。Setting the transfer mode to StreamedRequest or StreamedResponse enables streaming communication only in the indicated direction.

流传输消除了对大型内存缓冲区的需要,从而提高了服务的可伸缩性。Streamed transfers can improve the scalability of a service by eliminating the need for large memory buffers. 更改传输模式是否确实能够提高可伸缩性取决于所传输的消息大小。Whether changing the transfer mode actually improves scalability in practice depends on the size of the messages being transferred. 当针对大型消息使用流传输而不是缓存传输时,可伸缩性的提高应最为明显。Improvements in scalability should be most evident when large messages use streamed instead of buffered transfers.

默认情况下,HTTP、TCP/IP 和命名管道传输协议使用缓冲消息传输。By default, the HTTP, TCP/IP and named pipe transports use buffered message transfers. 您可以使用在 BasicHttpBindingNetTcpBindingNetNamedPipeBinding 系统提供的绑定上所公开的传输模式属性设置 TransferMode 的值。You can set the values of TransferMode on the BasicHttpBinding, NetTcpBinding, and NetNamedPipeBinding system-provided bindings using the transfer mode properties exposed on them. 可以通过使用 NetTcpBinding.TransferMode 属性在 NetTcpBinding 类上设置模式。The mode can be set on the NetTcpBinding class, for example, by using the NetTcpBinding.TransferMode property. 也可以在绑定的配置节中进行设置。It can also be set in the configuration section for the binding.

对于未公开传输模式属性的绑定,可以在传输的绑定元素上设置传输模式,此后可以将该元素添加到自定义绑定。For bindings that do not expose the transfer mode property, the transfer mode can be set on the binding element of the transport and this element can then be added to a custom binding. 例如,创建 HttpTransportBindingElement 并在创建自定义绑定时利用 TransferMode 属性设置传输模式。For example, create an HttpTransportBindingElement and use the TransferMode property to set the transfer mode when creating a custom binding. 也可以在自定义绑定的配置节中设置传输模式。The transfer mode can also be set in the configuration section for the custom binding.

对于 HTTP 传输,使用缓冲传输还是流传输是在终结点本地决定的。The decision to use either buffered or streamed transfers is a local decision of the endpoint for HTTP transports. 对于 HTTP 传输,传输模式不会通过连接传播,也不会传播到代理服务器或其他中间方。For HTTP transports, the transfer mode does not propagate across a connection, or to proxy servers or other intermediaries. 设置传输模式不会反映在服务协定的说明中。Setting the transfer mode is not reflected in the description of the service contract. 在对服务生成一个代理后,可以但不强求为旨在与流传输一起使用的服务编辑配置文件,以设置传输模式。After generating a proxy to a service, you can (it is allowed but not required) edit the configuration file for services intended to be used with streamed transfers to set the transfer mode. 对于 TCP 和命名管道传输协议,该传输模式将作为策略断言传播。For TCP and named pipe transports, the transfer mode is propagated as a policy assertion.

使用 Streamed 传输模式会导致 WCF 运行时强制实施某些限制。Using the Streamed transfer mode causes the WCF runtime to enforce some restrictions.

  • 在编程模型层,整个流传输中发生的操作最多只能与一个输入和/或输出参数之间具有协定。Operations that occur across a streamed transport can have a contract with at most one input and/or one output parameter at the programming model layer. 该参数对应于整个消息正文,并且必须为 MessageStream 的子类型或实现 IXmlSerializable 接口。That parameter corresponds to the entire body of the message and must be a Message, be a subtype of Stream, or implement the IXmlSerializable interface. 具有某个操作的返回值等效于具有一个输出参数。Having a return value for an operation is equivalent to having an output parameter.

  • 某些 WCF 功能(如可靠消息传递和 SOAP 消息级安全)依赖于缓冲消息以便进行传输。Some WCF features such as Reliable Messaging and SOAP message-level security rely on buffering messages for transmissions. 使用这些功能可能减小或消除通过使用流获得的性能好处。Using these features may reduce or eliminate the performance benefits gained by using streaming. 若要保证流传输的安全,请只使用传输级安全,或者使用 WS-Security 声明外加传输安全的混合模式安全。To secure a streamed transport, use transport level security only or use mixed-mode security, which combines WS-Security claims with transport security.

  • 即使传输模式设置为 Streamed,也总是会对 SOAP 标头进行缓冲。SOAP headers are always buffered, even when the transfer mode is set to Streamed. 消息的标头不得超过在各种绑定和绑定元素上公开的 MaxBufferSize 传输配额大小。The headers for a message must not exceed the size of the MaxBufferSize transport quota which is exposed on the various bindings and binding elements.

将传输模式从 Buffered 更改为 Streamed 还会更改 TCP 和命名管道传输协议的本机通道形状。Changing the transfer mode from Buffered to Streamed also changes the native channel shape of the TCP and named pipe transports. 对于缓冲传输模式,本机通道形状为 IDuplexSessionChannelFor buffered transfers, the native channel shape is IDuplexSessionChannel. 对于流传输模式,本机通道为 IRequestChannelIReplyChannelFor streamed transfers, the native channels are IRequestChannel and IReplyChannel. 因此,会话服务协定不能与传输流一起使用。A consequence of this is that sessionful service contracts cannot be used with transport streaming.