Transfer mode
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
Buffer 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.
Stream transfer
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.
Configuration
<system.serviceModel> <services > <service behaviorConfiguration="ServiceBehavior" name="MyService"> <endpoint address="" binding="netTcpBinding" bindingConfiguration="MyService.netTcpBinding" contract="IMyService"> <identity> <dns value="localhost"/> </identity> </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true "/> </behavior> </serviceBehaviors> </behaviors> <bindings > <netTcpBinding> <binding name="MyService.netTcpBinding" transferMode="Buffered" closeTimeout ="0:01:00" openTimeout="0:01:00"></binding> </netTcpBinding> </bindings> </system.serviceModel>
Differences between Buffered and Streamed Transfers
Buffered | Streamed |
---|---|
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 |
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(); }
- Stream and it's subclass can be used for streaming, but it should be serializable
- Stream and MemoryStream are serializable and it will support streaming
- 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