How do I configure a WCF service to run with PerCall instance context?

1.3k Views Asked by At

Here's the background info. I have a WCF service configured like so

   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

Now I need some help understanding exactly how this context mode works.

Here's the problem I'm dealing with. For the sake of argument, lets say I have a call that takes 10 seconds to execute and return from the server. If I make two calls simultaneously (using Ajax request), the first request will come back after 10 seconds. But the second request will come back after 20 seconds.

I was expecting that this context mode would process both requests independent of each other, and return both at the same time. Have I configured my service properly?

2

There are 2 best solutions below

0
On

The following configuration

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

may not always create service contexts immediately for each call. If the binding that is used is session enabled and the service implements IDispose, then a client call will be queued while there is already a service instance for another client. So, check binding details in your case.

0
On

In PerCall each call is assigned a separate instance (for extreme scalability). Every call to the method results in a new server object being created (separate thread). Once the request has been processed (method returns), the new instance is released.

If you had configured the method to use ConcurrencyMode.Single then you would see this blocking behavior.

You should consider reviewing the serviceThrottling configuration to ensure that your application is tuned properly.

<serviceBehaviors>
    <behavior name="throttleBehavior">
        <serviceThrottling maxConcurrentCalls="15" maxConcurrentInstances="10"/>
    </behavior>
</serviceBehaviors>