How does Concurrency work in WCF?

前端 未结 5 1098
天涯浪人
天涯浪人 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.

提交回复
热议问题