Friday, May 25, 2012

WCF - Throttling


Throttling

WCF throttling provides some properties that user can use to limit how many instances or sessions are created at the application level.
Performance of the WCF service can be improved by creating proper instance
maxConcurrentCalls  : It limits the number of concurrent requests or calls that can be processed by WCF service instances,default value is 16.
maxConcurrentInstances :It limits the number of service instances that can be allocated at a given time  or processed by service host.Default value =32
maxConcurrentSessions : It limits the number of active sessions allowed for the service. Default Value=10

WebConfig Example :

<system.serviceModel>
    <services>
      <service behaviorConfiguration="DuplexWcfService.Service1Behavior" name="CallBackOperation.DuplexCallBack">
        <endpoint address="http://localhost:62345/DuplexCallBack.svc" binding="wsDualHttpBinding" contract="CallBackOperation.IDuplexServiceContract">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DuplexWcfService.Service1Behavior">
           <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true" />

<serviceThrottling maxConcurrentCalls="500" maxConcurrentInstances ="100" maxConcurrentSessions ="200"/>

        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>


Programically :
class Program
    {
        static void Main(string[] args)
        {
            ServiceHost myService = new ServiceHost(typeof(WCFCallbacks.MessageService));

                          
            ServiceThrottlingBehavior throttleBehavior = new ServiceThrottlingBehavior
            {
                MaxConcurrentCalls = 40,
                MaxConcurrentInstances = 20,
                MaxConcurrentSessions = 20,
            };
            myService.Description.Behaviors.Add(throttleBehavior);
            myService.Open();
            Console.WriteLine("Press enter to stop.");         
            myService.Close();


            myService.Open();
            Console.WriteLine("Press enter to stop.");
             myService.Close();
        }
    }




Concurrency Mode


Concurrency issues arise when multiple threads attempt to access the same resources at run time.
if multiple clients call the same service, multiple concurrent request threads can arrive for a service.
Service object handling each request is based on the instancing mode for the service.
PerCall services : A new service object is granted for each request.
 PerSession : One service object receives requests from the same client (or, proxy).
Single instancing mode : All client requests are sent to the same singleton service object.
There are three ways by which you can handle concurrency in WCF:

Single: Only one request will be processed at any given moment of time. The other requests have to wait until the request processed by the WCF service is completed. By default, services are configured for Single concurrency mode.
This means that a lock is acquired for the service object while a request is being processed by that object. Other calls to the same object are queued. When the request that owns the lock has completed, and thus released the lock, the next request in the queue can acquire the lock and begin processing. This configuration reduces the potential throughput at the service, when sessions or singletons are involved, but it also yields the least risk for concurrency issues.
Eg :
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,ConcurrencyMode=ConcurrencyMode.Single)]
Configuring services for Single access doesn’t impact PerCall services because a new service instance is allocated for each request.

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession ,ConcurrencyMode=ConcurrencyMode.Single)]
  In  Single instancing mode, only one request can be processed across all clients so it doesn’t  throughput of multiple clients.

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single ,ConcurrencyMode=ConcurrencyMode.Single)]
In this combination, only one instance of a WCF service instance is created which serves all requests which are sent from  clients.
Multiple: In this scenario, multiple requests can be handled by the WCF service object at any given moment of time
 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,ConcurrencyMode=ConcurrencyMode.Multiple)]
 In this combination singlt threads for all clients requests

 [ServiceBehavior(InstanceContextMode =InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]
  In this combination Multiple threads for every request by same client or proxy

 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode=ConcurrencyMode.Multiple)]
  In this combination Multiple threads for all clients requests handle by single service instance

Reentrant: A single request thread has access to the WCF service object, but the thread can exit the WCF service to call another WCF service or can also call a WCF client through callback and reenter without deadlock.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
Single threads for all clients, locks are released when calls diverted to other WCF services
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
Single threads for all clients, locks are released when calls diverted to other WCF services.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
Single threads for all clients, locks are released when calls diverted to other WCF services.


No comments :

Post a Comment