Operation synchronization different from class synchronization
I have a WebService (.svc) class with
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]
and I want to make some of it operations multiple not single and some single.
H开发者_StackOverflow社区ow can I do it?
You could mark class as ConcurrencyMode.Multiple, make private synchronization field, and lock on that field on method entrancies which need to be Single.
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
class SomeSvc : ContractInterface
{
private readonly Object _syncLock = new Object();
// this method is ConcurrencyMode.Single
public String SingleMethod1()
{
lock (_syncLock)
{
// method code here
}
}
// this method is ConcurrencyMode.Single
public String SingleMethod2()
{
lock (_syncLock)
{
// method code here
}
}
// this method is ConcurrencyMode.Multiple
public String MultipleMethod()
{
// ...
}
}
You can't use two different concurrency configurations in single service. As you can see the attribute is called ServiceBehavior
= it affects whole service. If you want two different behaviors you must divide your code into two services. Also there is no InstanceContextMode
specified in your attribute so there is possiblity that setting ConcurrencyMode
to Multiple
will have no effect.
精彩评论