How does Concurrency work in WCF?
I am a novice in WCF and SOA. I am just starting out on these, I have a theoretical doubt:
Client A has called a service and the logic is currently executing on the server. While the logic is executing, another call from Client B comes in for the same service.
At this point what happens to the logic that is being executed for Client A? How does the service manage to serve both the requests?
Answer to your question depends on binding you are using. There are two settings controlling this behavior: InstanceContextMode and ConcurrencyMode. Both these settings are set in ServiceBehaviorAttribute.
InstanceContextMode controls how the service is instantiated. It has following values:
PerCall - each time you call the service new service instance is created. This is default behavior for services exposed on bindings which don't use transport session, reliable session or security session => BasicHttpBinding, WebHttpBinding.
PerSession - each time you call the service from new proxy instance new service instance is created. Any subsequent call from the same proxy is handled by the same service instance (instance lives on the server). By default subsequent call has to be done within 10 minutes (receiveTimeout) or service instance is released. This is default default behavior for services exposed on binding which use transport session, reliable sesion or security session => WSHttpBinding (default setting uses security session), NetTcpBinding, NetNamedPipeBinding.
Single - only one instance of the service exists and it handles all calls. This service instance can be created when host starts or when service is called first time.
Now you know how instances are created. The second setting ConcurrencyMode controls how many concurrent threads can access single instance. Each request is always handled in separate thread.
Single - only one thread can access service instance. This is default behavior.
Reentrant - one thread can access service but but it can release the lock and allow other thread to use the instance while firts thread will be blocked. This is used in callback scenario.
Multiple - multiple threads can access service instance.
Now you know how instance can be concurrently used. Lets have a look on some combinations:
PerCall instancing + Single concurrency - typical stateless scenario. Multiple concurrent calls are allowed.
PerCall instancing + Multiple concurrency - doesn't make sense. It still behaves like Single concurrency.
PerSession instancing + Single concurrency - multiple concurrent calls are allowed but only single call from each proxy can be processed at the same time. Other calls are queued.
PerSession instancing + Multiple concurrency - multiple concurrent calls are allowed. Multiple calls from each proxy can access same instance at the same time. You have to do manual synchronization of access to shared fields in the service instance.
Single instancing + Single concurrency - only single request can be processed at time. Other requests are queued (default timeout 30s).
Single instancing + Multiple concurrency - multiple concurrent calls are allowed. All calls access same instance at the same time. You have to do manual synchronization of access to shared fields in the service instance.
That depends on the value of the ConcurrencyMode property of the ServiceBehavior attribute that is applied to the service implementation. If the ConcurrencyMode is Single, the call from Client B will wait for the call from Client A to complete; if the ConcurrencyMode is Multiple, both will be executed at the same time but on separate threads.
If not set, the ConcurrencyMode defaults to Single: http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.concurrencymode.aspx
You may also find the InstanceContextMode property useful for understanding and controlling how multiple requests from multiple clients are handled: http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.instancecontextmode.aspx
There are settings for both instancing and concurrency, see http://msdn.microsoft.com/en-us/library/ms731193.aspx for details.
Running your WCF service in IIS takes care of some of the concurrency issues.
You can control the concurrency using the ConcurrencyMode and control how new connections are handled via InstanceContextMode - this is the Microsoft documentation.
The number of concurrent connections can also be throttled - have a look at the serviceThrottling element in the your WCF Config.
Well the service is executing the requests in seperate Threads.
So they might get executed at the same time.