Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF - Handling request from multiple clients

Tags:

wcf

clients

My WCF service library is hosted as a Windows Service and is supposed to handle requests from multiple clients. One request that clients are going to make frequently is pretty resource intensive.

I have two doubts pertaining to above scenerio:

  1. How a WCF service handles multiple client's request?
  2. Is there any WCF configuration to make the process efficient?

Thank you!

like image 596
inutan Avatar asked Oct 09 '09 10:10

inutan


1 Answers

In your default scenario, the WCF service host (the thing hosting your service class) will create a new instance of your service class for each request that comes in, and lets that handle the request ("per-call" activation).

You can tweak the maximum number of those concurrently active service class instances using the serviceThrottling behavior on your server.

<system.serviceModel>
   <behaviors>
      <serviceBehaviors>
         <behavior name="ThrottledServiceBehavior">
            <serviceThrottling 
                maxConcurrentCalls="25" 
                maxConcurrentSessions="25"
                maxConcurrentInstances="25"/>
         </behavior>
      </serviceBehaviors>
   </behaviors>

There's a really good explanation of the options of the service throttling behavior and its default values in Kenny Wolf's blog post here.

Also, setting of the InstanceContextMode and ConcurrencyMode on your service class (that implements the service contract) have a strong influence on how your service will handle concurrency and multiple requests.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall, 
                 ConcurrencyMode=ConcurrencyMode.Single)]
class YourServiceClass : IYourService
{
  .....
}

InstanceContextMode should be PerCall (each calling request gets a new, separate instance) and then ConcurrencyMode can be Single (which is the easiest to develop).

InstanceContextMode could also be PerSession if you need a session-based approach (not very common), or Single (your service class would a singleton - highly discouraged to use this, unless you absolutely, positively have to and know about all the quirks and problems with it!).

ConcurrencyMode could also be Reentrant (only relevant for duplex contracts and bindings) or Multiple (multithreaded singleton service class - highly risky and difficult to develop!).

Marc

like image 187
marc_s Avatar answered Sep 21 '22 20:09

marc_s