Streaming in WCF

Objective

In this article, I am going to explain various aspects of streaming of messages in WCF. For example :

  1. I/O Streams
  2. Streaming and Binding
  3. Streaming and Transport channel
  4. Stream management

Why we need Streaming?

In WCF any receiving message is delivered only once entire message has been received. What I mean here is that first message is buffered at the receiving side and once it is fully received it gets delivered to the receiving end. Main problem with this approach is that receiver end is unresponsive while message is getting buffered. So default way of message handling in WCF is ok for small size messages but for the large size messages this approach is not good. So to overcome this problem Streaming in WCF come into action.

I/O Streams

WCF uses .Net Stream class for the purpose of streaming. There are no any changes while defining contract. It simply looks like normal contracts. Below contract is returning stream and taking stream input.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;

namespace OneWayService
{

    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        Stream ReturnStreamfromSeervice();

        [OperationContract]
        void ProcessStreamFromClient(Stream stream);

    }
}

  1. Parameter could be defined either as abstract base class Stream or Subclass MemoryStream
  2. Subclasses FileStream etc are not allowed as the type of parameters.

Steaming and Binding

  1. TCP, IPC and HTTP bindings support streaming.
  2. For all the Binding streaming is disabled by default.
  3. Streaming of the message should be enabled in the binding to override the buffering and enable the streaming.
  4. TransferMode property should be set according to the desired streaming mode in the bindings.
  5. Type of TransferMode property is enum TransferMode

    TransferMode enum type

    using System;
    namespace System.ServiceModel
    {

        public enum TransferMode
        {

            Buffered = 0,
            Streamed = 1,
            StreamedRequest = 2,
            StreamedResponse = 3,
        }
    }

     
  6. This property is defined in all the binding classes.

    TransferMode property in BasicHttpBinding class.

    using System;
    using System.ServiceModel.Channels;
    using System.Text;
    using System.Xml;

    namespace System.ServiceModel
    {
        public class BasicHttpBinding : Binding, IBindingRuntimePreferences
        {

            public TransferMode TransferMode { get; set; }
        }
    }

     

  7. TransferMode.Streamed supports all type of streaming modes. This supports all stream operations like receiving, returning.

  8. If you want to have buffer request and stream response TransferMode.StreamedResponse.

Configuring Streaming in Config file

<system.serviceModel>
                        <
services>
                                    <
service name="OneWayService.Service1" behaviorConfiguration="OneWayService.Service1Behavior" >
                                                <!-- Service Endpoints -->
                                                <
endpoint address="" binding="basicHttpBinding" bindingConfiguration="StreamedHttp" contract="OneWayService.IService1" >
                                                           
                                                            <
identity>
                                                                        <
dns value="localhost"/>
                                                            </identity>
                                                </
endpoint>
                                                <
endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                                    </service>
                        </
services>
    <
bindings>
      <
basicHttpBinding>
        <
binding name ="StreamedHttp" transferMode ="Streamed" />
      </basicHttpBinding>
    </bindings>
                        <
behaviors>
                                    <
serviceBehaviors>
                                                <
behavior name="OneWayService.Service1Behavior">
                                                                                                                        <serviceMetadata httpGetEnabled="true"/>
                                                                                                                        <serviceDebug includeExceptionDetailInFaults="false"/>
                                                </behavior>
                                    </
serviceBehaviors>
                        </
behaviors>
            </system.serviceModel>

Increasing Message size

The default message size is 64K. But the message size in case of streaming may be very large. So default message size could be increased.
MaxReceivedMessageSize property of BasicHttpBinding class is used to increase the message size. Typical way of increasing message size is in config file.

<basicHttpBinding>
        <
binding name ="StreamedHttp" transferMode ="Streamed"  maxReceivedMessageSize ="100000"/>
      </basicHttpBinding>

Conclusion

I have explained all the facets related to Streaming in WCF. Thanks for reading.
Happy Coding
 

Up Next
    Ebook Download
    View all
    Learn
    View all