Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Concurrency work in WCF?

Tags:

.net

soa

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?

like image 702
Sandeep Avatar asked Sep 07 '10 10:09

Sandeep


People also ask

What is concurrency in WCF?

There are three concurrency modes available: Single : Each service instance processes one message at a time. This is the default concurrency mode. Multiple : Each service instance processes multiple messages concurrently. The service implementation must be thread-safe to use this concurrency mode.

Is WCF multithreaded?

WCF does not create any queue for client messages and replays them as soon as they arrive. Each service has multiple threads processing messages concurrently.

What is throttling in WCF?

Throttling controls place limits on the number of concurrent calls, instances, or sessions to prevent over-consumption of resources. Throttling behavior is specified in service configuration file settings. This sample is based on the Getting Started that implements a calculator service.

What is default instance mode in WCF?

Per-call service is the default instance activation mode of WCF. When a WCF service is configured for a per-call service, a CLR object is created for the timespan a client call or request is in progress.


2 Answers

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.

like image 107
Ladislav Mrnka Avatar answered Sep 28 '22 04:09

Ladislav Mrnka


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

like image 45
FacticiusVir Avatar answered Sep 28 '22 03:09

FacticiusVir