.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Streaming of messages in WCF

Posted By:Dhananjay Kumar       Posted Date: January 25, 2010    Points: 25    Category: WCF    URL: http://www.dotnetspark.com  

In this article, I am going to explain various aspects of streaming of messages in WCF. I/O Streams, Streaming and Binding, Streaming and Transport channel, Stream management
 

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


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
    
  
  

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.


  
  

Conclusion

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

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend