Streaming in WCF

Introduction: In this blog, I will describe what is streaming in WCF.

Streaming is a technique of transferring message using Streaming transfer mode. In WCF, there are two mode of transferring message which are these:

  1. Buffered transfer
  2. Streamed transfer

Buffered Transfer

Buffered transfers hold the entire message in a memory buffer until the transfer is complete. A buffered message must be completely delivered before a receiver can read it.

For buffered transfers, the native channel shape is IDuplexSessionChannel.

Streamed Transfer

Streamed transfers expose the message as a stream. The receiver starts processing the message before it is completely delivered.

For streamed transfer, the native channels are IRequestChannel and IReplyChannel.

Benefits of Streamed Transfer

  1. Improve the scalability.
  2. No need of large memory buffer.
  3. It can transfer large messages.

Restrictions to use Streaming in WCF

  • Streaming is not available with the Message Queuing (MSMQ) transport.
  • It is not available for Peer Channel transport.
  • 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 sent messages on the client for redelivery.

I/O Stream

  • WCF uses .Net stream class for Streaming the message.
  • Stream in base class for streaming, all subclasses like FileStream, MemoryStream, NetworkStream are derived from it.
     

Code:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    void SaveStreamData(Stream emp);
 
    [OperationContract]
    Stream GetStreamData();
 

}

Some Important things about streaming

  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.