I have 3 web methods in a WCF, called 'DeleteGroup', 'AddGroup', 'ModifyGroup'.
Each method has its own code. I want to place a lock on code within each method, so that only one method executes at a time. For example, if 'DeleteMethod' is being executed then no request for 'AddGroup' or 'ModifyGroup' can be executed. So may be I could
use something like what is below, but not very sure ? Also, where in the WCF would I declare the locker object, and would it be private, public or static (i.e. shared)?
I think the concurrency you have described will make the entire instance of WCf a single threaded app. I want concurrent calls to methods allowed provided these methods get data/change data that are not inter-related. For example, the 'AddGroup' method and
the 'AddCustomer' methods can be executed at the same time since their actions are completely independent of each other.
So it seems the WCF solution that you have recommended is not a good approach for my case since it will slow down the WCF response.
sun21170
Contributor
3421 Points
1189 Posts
Locking multiple sections of code with same lock
May 30, 2011 07:29 PM|LINK
I have 3 web methods in a WCF, called 'DeleteGroup', 'AddGroup', 'ModifyGroup'.
Each method has its own code. I want to place a lock on code within each method, so that only one method executes at a time. For example, if 'DeleteMethod' is being executed then no request for 'AddGroup' or 'ModifyGroup' can be executed. So may be I could use something like what is below, but not very sure ? Also, where in the WCF would I declare the locker object, and would it be private, public or static (i.e. shared)?
SyncLock locker
--Delete Method Body
End SyncLock
SyncLock locker
--Modify Method Body
End SyncLock
SyncLock locker
--Add Method Body
End SyncLock
akhhttar
Contributor
6506 Points
963 Posts
Re: Locking multiple sections of code with same lock
May 30, 2011 07:49 PM|LINK
Hi,
To perform locking in WCF Service you have to use ConcurrencyMode attribute with your service methods.
"When the service is set with ConcurrencyMode.Single, WCF will provide automatic synchronization to the service instance and disallow concurrent calls by associating the service instance with a synchronization lock". Please see http://codeidol.com/csharp/wcf/Concurrency-Management/Service-Concurrency-Mode/
Thanks
-Akhtar
My Blog
sun21170
Contributor
3421 Points
1189 Posts
Re: Locking multiple sections of code with same lock
May 30, 2011 08:21 PM|LINK
I think the concurrency you have described will make the entire instance of WCf a single threaded app. I want concurrent calls to methods allowed provided these methods get data/change data that are not inter-related. For example, the 'AddGroup' method and the 'AddCustomer' methods can be executed at the same time since their actions are completely independent of each other.
So it seems the WCF solution that you have recommended is not a good approach for my case since it will slow down the WCF response.
Do you have any other ideas?
Thanks
sun21170
Contributor
3421 Points
1189 Posts
Re: Locking multiple sections of code with same lock
May 30, 2011 08:29 PM|LINK
Hi Akhtar,
Also, my WCF has the following configuration, and so synchronization is provided by WCF only for the instance created for the WCF:
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerCall, ConcurrencyMode:=ConcurrencyMode.Single)>
Thanks