assume I have a hosted wcf service in a console application like this:
public class MyService : IContract
{
public static readonly object _locker_ = new object();
public static void DoSomething()
{
lock (_locker_)
{
//block 1
DoAnotherWork();
}
}
void IContract.Foo1()
{
lock (_locker_)
{
//block 2
DoSomeWork();
}
}
void IContract.Foo2()
{
DoSomething();
}
}
does this insures that only one client is in block 1? or block 2?
A static lock object is per-appdomain; so “block 1” and “block 2” only allow a single caller. Ultimately, yes. As long as you only have one appdomain ;p
Note that locks are re-entrant, so the thread with the lock can call the methods again without deadlocking itself (but not via WCF; only via regular C# method calling).