qkhanhpro
qkhanhpro

Reputation: 5220

Long running WCF service block others

I had InstanceContextMode set to PerSession/PerCall through ServiceHost

Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.PerSession;
Description.Behaviors.Find<ServiceBehaviorAttribute>().UseSynchronizationContext = false;
ServiceThrottlingBehavior stb = new ServiceThrottlingBehavior
{
    MaxConcurrentSessions = 1000,
    MaxConcurrentCalls = 1000,
    MaxConcurrentInstances = 1000
};
Description.Behaviors.Add(stb);

New service instance gets created for each call. However, as I know, WCF services use threads from ThreadPool, those requests will be queued to available threads until it decides to start new one. When there is a service with long running operation, it blocks ( some? ) subsequent requests from being processed.

I even tried to set ThreadPool.SetMaxThreads() and ThreadPool.SetMinThreads() to high value but still no luck.

So, what is good pratice to handle this kind of problem?

Edit: I tried different combination of PerSession/PerCall with ConcurrencyMode but it did not work. Its only non-blocking when i raise the number or Maximum Worker Process in IIS setting

Edit after quite some time : Now I understand that its not recommended to run long-running actions inside WCF service because it uses ThreadPool. Since one blocking request will cause all subsequent requests ( that are automatically queued to some limitted thread amount, until ThreadPool decide that it should spawn more thread ). So i'm leaving it here for people who faces same problem.

Upvotes: 1

Views: 1220

Answers (1)

jtabuloc
jtabuloc

Reputation: 2535

If you set InstanceContextMode.PerSession as InstanceContextMode it instructs the service application to create a new service object when a new communication session is established between a client and the service application. Subsequent calls in the same session are handled by the same object.

The main reason why succeeding request from same client got block because it has something to do with ConcurrencyMode which is by default Single. What does it mean? It means that no request/calls can interrupt ongoing process of the service because it use only single thread to process the first request other succeeding request got block or queued waiting for service to be available and it will get timeout eventually if the first process is not yet finish. In addition service doesn't allow re-entrant.

If you want to understand if fully see links below because it interact to each other.

InstanceMode and Concurrency Model

Upvotes: 1

Related Questions