Do I need to implement my own locking in a WCF service that uses ConcurrencyMode.Multiple
and InstanceContextMode.PerCall
or InstanceContextMode.PerSession
? Since a new ServiceContext
object is created at every call or new session I should think that I would not, but I'm far from sure.
Example:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerSession,
Include开发者_如何学JAVAExceptionDetailInFaults = false, MaxItemsInObjectGraph = Int32.MaxValue)]
public class ExampleService : IExample
No you don't have to add locking. Each call will get a fresh instance.
However, if you need state from a particular caller, that will have to be handled manually.
See this thread for more information
If you use a PerCall instantiation, you don't need to worry about the concurrent mode, because only one request can use the instance, so you won't have lock issues.
For PerCall, if your client uses sessions and is able to send multiple requests at the same time (say, using the same proxy from numerous threads) then yes, you will need to lock objects which are not thread-safe. I guess you're using PerSession because you want to preserve state, so you'll need to lock your state changing methods/code.
精彩评论