Saturday 28 January 2012

Streaming

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.

Supported Bindings

  • BasicHttpBinding
  • NetTcpBinding
  • NetNamedPipeBinding

Restrictions

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

I/O Streams

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. Stream the data, you need to do is, to return or receive a Stream as an operation parameter.
[ServiceContract]
public interface IMyService
{
    [OperationContract]
    void SaveStreamData(Stream emp);

    [OperationContract]
    Stream GetStreamData();

}
Note:
  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
{
   Buffered, //Default
   Streamed,
   StreamedRequest,
   StreamedResponse
}
public class BasicHttpBinding : Binding,...
{
   public TransferMode TransferMode
   {get;set;}
   //More members
}
  • 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.
<system.serviceModel>
    <bindings >
      <netTcpBinding>
        <binding name="MyService.netTcpBinding"
         transferMode="Buffered" maxReceivedMessageSize="1024000">
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>

No comments:

Post a Comment