ObjectiveIn this article, I am going to explain various aspects of streaming of messages in WCF. For example :
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);
}}
Steaming and Binding
namespace System.ServiceModel{ public class BasicHttpBinding : Binding, IBindingRuntimePreferences { public TransferMode TransferMode { get; set; } }}
TransferMode.Streamed supports all type of streaming modes. This supports all stream operations like receiving, returning.
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 sizeThe 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>ConclusionI have explained all the facets related to Streaming in WCF. Thanks for reading. Happy Coding
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: