Lock using ConcurrencyMode.Multiple and InstanceContextMode.PerCall

Do I need to implement my own lock in a WCF service that uses ConcurrencyMode.Multiple and InstanceContextMode.PerCall or InstanceContextMode.PerSession ? Since a new ServiceContext is created with every call or new session, I should think that I will not, but I am far from certain.

Example:

 [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession, IncludeExceptionDetailInFaults = false, MaxItemsInObjectGraph = Int32.MaxValue)] public class ExampleService : IExample 
+4
source share
2 answers

No, you do not need to add a lock. Each call will receive a new instance.

However, if you need state from a specific caller, this must be handled manually.

See thread for more information.

+2
source

If you are using an instance of PerCall, you do not need to worry about parallel mode, because only one request can use the instance, so you will not have problems with blocking.

For PerCall, if your client uses sessions and can send several requests at the same time (say, using the same proxy from multiple threads), then yes, you will need to block objects that are not thread safe. I assume that you are using PerSession because you want to save state, so you need to lock your state change methods / code.

+2
source

All Articles