WCF (Windows Communication Foundation) is a secure, reliable, and scalable messaging platform for developing services in .Net. It provides support for two modes of transferring data: the buffered mode and the streamed mode. While in the buffered mode (this is the default mode supported by WCF) the data is stored in the buffer in its entirety until the data transfer is complete. In the streamed mode, the data is read in chunks, and the receiver of the message can start processing the message until the transfer is complete.
While the buffered mode is the default and needs the message to be sent to the receiver in its entirety before the message can be processed at the consumer's end, the streamed mode is useful for messages that are large in size. Processing large chunks of data requires a large buffer and a lot of available memory both at the client and also at the server side, hence you need to use streamed mode, which improves the scalability of the service as you don't need to have large memory buffers to hold data.
To enable the any of the modes described above, you need to take advantage of the TransferMode property. This property can have any one of the following values.
- Buffered -- enables buffered mode
- Streamed -- enables streamed mode of bi-directional communication
- StreamedRequest -- enables streamed mode for the request only
- StreamedResponse -- enables streamed mode for the response only
Note that the HTTP, TCP/IP, and named pipe transports use buffered transfers by default and the streamed transfer mode needs to be turned on explicitly in the service configuration file. The streamed mode is supported only by the basicHttpBinding, netTcpBinding, and netNamedPipeBinding bindings; the TransferMode is an enum defined in the System.ServiceModel namespace.
public enum TransferMode
Buffered = 0,
Streamed = 1,
StreamedRequest = 2,
StreamedResponse = 3,
The following code snippet shows how the TransferMode property can be set in the service configuration file.
The default message size is 64K (the maximum size it can support is 64MB), but you should increase the message size if you need to use streamed mode and transfer large chunks of data. Here's how you can configure the message size in the service configuration file.
<binding name="customStreamedBinding" transferMode="Streamed"
Sign up for CIO Asia eNewsletters.