Throttling In WCF

The word throttling is mainly derived from the throughput keyword. Throughput means that the work performs in a given specific time interval. The throughput of any WCF Service mainly depends on the number, for instance, the number of sessions you create. Based on the number of instance and session, you can increase and decrease the performance for WCF Service.


Controlling the number of instance and session for WCF Service, we have to use throttling. In throttling, you have to specify the number for the concurrent call and concurrent session, and the concurrent instance allows for the client to communicate with WCF Service. In throttling, you can specify it in  two ways:  in the app.config file of the Service or you can specify it at the hosting time programmatically.

Config File
  1. <behaviors>  
  2.   <serviceBehaviors>  
  3.     <behavior name="throttlingBehavior">  
  4.       <serviceThrottling  
  5.         maxConcurrentCalls="2"  
  6.         maxConcurrentInstances="2"  
  7.         maxConcurrentSessions="2"/>  
  8.     </behavior>  
  9.   </serviceBehaviors>  
  10. </behaviors>  
Programmatically
  1. ServiceHost host = new ServiceHost(typeof(ThrottlingInWCF.ThrottlingInWCF));  
  2.   
  3. ServiceThrottlingBehavior stb= new ServiceThrottlingBehavior();  
  4. stb.MaxConcurrentCalls=2;  
  5. stb.MaxConcurrentInstances=2;  
  6. stb.MaxConcurrentSessions=2;  
  7.   
  8. host.Description.Behaviors.Add(stb);  
  9. host.Open();  
  10. Console.Write("Service started...");  
  11. Console.Read();  
In the commands, shown above, we specify

maxConcurrentCalls="2"
maxConcurrentInstances="2"
maxConcurrentSessions="2"
  • maxConcurrentCalls="2"
    Here, this will show that the client can create a maximum number of 2 concurrent calls to communicate with WCF Service.

  • maxConcurrentInstances="2”
    Here, this will specify the client can create a maximum of 2 concurrent instances to communicate with WCF Service.

  • maxConcurrentSessions="2"
    Here, this will specify the client can create a maximum of 2 concurrent sessions to communicate with WCF Service.

It means the client only creates maximum number for the concurrent calls, sessions and instances as per throttling behavior. If the client crosses the limit, then the Service will not allow doing this.

As shown above, you can specify the throttling in two ways; i.e., in config file and at hosting time. Thus, we can say that using throttling in WCF, you can create hurdles for the client to communicate or access the WCF Service.

Ebook Download
View all
Learn
View all