Daniel Gomez Rico
Daniel Gomez Rico

Reputation: 15945

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.

How can I do it?

Upvotes: 0

Views: 127

Answers (2)

Loki Kriasus
Loki Kriasus

Reputation: 1301

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()
    {
        // ...
    }
 }

Upvotes: 2

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364389

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.

Upvotes: 1

Related Questions