In our normal day today life, we need to transfer data from one location to other location. If data transfer is taking place through WCF service, message size will play major role in performance of the data transfer. Based on the size and other condition of the data transfer, WCF supports two modes for transferring messages:1) Buffer Transfer
2) Stream Transfer
When the client and the service exchange messages, these messages are buffered on the receiving end and delivered only once the entire message has been received. This is true whether it is the client sending a message to the service or the service returning a message to the client. As a result, when the client calls the service, the service is invoked only after the client's message has been received in its entirety; likewise, the client is unblocked only once the returned message with the results of the invocation has been received in its entirety.
When client and Service exchange message using Streaming transfer mode, receiver can start processing the message before it is completely delivered. Streamed transfers can improve the scalability of a service by eliminating the requirement for large memory buffers. If you want to transfer large message, streaming is the best method.
StreamRequest: In this mode of configuration, message send from client to service will be streamed.
StreamRespone: In this mode of configuration, message send from service to client will be streamed.
<service behaviorConfiguration="ServiceBehavior" name="MyService">
<endpoint address="" binding="netTcpBinding"
<endpoint address="mex" binding="mexHttpBinding"
<serviceDebug includeExceptionDetailInFaults="true "/>
transferMode="Buffered" closeTimeout ="0:01:00" openTimeout="0:01:00"></binding>
Differences between Buffered and Streamed Transfers
Target can process the message once it is completely received.
Target can start processing the data when it is partially received.
Performance will be good when message size is small
Performance will be good when message size is larger (more than 64K)
Native channel shape is IDuplexSessionChannel
Native channels are IRequestChannel and IReplyChannel
Client and Service exchange message using Streaming transfer mode, receiver can start processing the message before it is completely delivered. Streamed transfers can improve the scalability of a service by eliminating the requirement for large memory buffers. If you want to transfer large message, streaming is the best method.
There are some restriction, when streaming is enabled in WCF:
- Digital signatures for the message body cannot be performed.
- Encryption depends on digital signatures to verify that the data has been reconstructed correctly.
- Reliable sessions must buffer sent messages on the client for redelivery if a message gets lost in transfer and must hold messages on the service before handing them to the service implementation to preserve message order in case messages are received out-of-sequence.
- Streaming is not available with the Message Queuing (MSMQ) transport.
- Streaming is also not available when using the Peer Channel transport.
WCF uses .Net stream class for Streaming the message. Stream is base class for streaming, all subclasses like FileStream, MemoryStream, NetworkStream are derived from it. Stream the data, you need to do is, to return or receive a Stream as an operation parameter.
public interface IMyService
void SaveStreamData(Stream emp);
1) Stream and it's subclass can be used for streaming, but it should be serializable.
2) Stream and MemoryStream are serializable and it will support streaming.
3) FileStream is non serializable, and it will not support streaming.
Streaming and Binding:
Only the TCP, IPC, and basic HTTP bindings support streaming. With all of these bindings streaming is disabled by default.TransferMode property should be set according to the desired streaming mode in the bindings.
public enum TransferMode
public class BasicHttpBinding : Binding,...
public TransferMode TransferMode
· StreamedRequest - Send and accept requests in streaming mode, and accept and return responses in buffered mode.
· StreamResponse - Send and accept requests in buffered mode, and accept and return responses in streamed mode.
· Streamed - Send and receive requests and responses in streamed mode in both directions.
· Buffered - Send and receive requests and responses in Buffered mode in both directions.
Streaming and Transport:
The main aim of the Streaming transfer mode is to transfer large size data, but default message size is 64K. So you can increase the message size using maxReceivedMessageSize attribute in the binding element as shown below.