How does Concurrency work in WCF?

前端 未结 5 1093
天涯浪人
天涯浪人 2021-01-31 21:57

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 se

相关标签:
5条回答
  • 2021-01-31 22:01

    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.

    0 讨论(0)
  • 2021-01-31 22:08

    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

    0 讨论(0)
  • 2021-01-31 22:12

    Well the service is executing the requests in seperate Threads.

    So they might get executed at the same time.

    0 讨论(0)
  • 2021-01-31 22:16

    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.

    0 讨论(0)
  • 2021-01-31 22:20

    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.

    0 讨论(0)
提交回复
热议问题