C# Web Service Mutual Exclusion
elI have two types of nodes. One is a coordinator 开发者_运维知识库and another is a worker node.
They communicate through SOAP.
All worker nodes send requests to the coordinator for a mutually exclusive database record.
I want the coordinator to return the ID of a safe record to each worker so no worker has the same record to process.
I know how to do something similar with threads in C# using mutex, but I wanted to know the best practice for synchronous web service calls.
EDIT: heres the problem I have
public class Coordinator: System.Web.Services.WebService
{
[WebMethod]
//Returns a mutually record to worker nodes
public int RequestRecordID()
{
//i want to lock here
int id = repository.getFirst(el => el.locked==false);
repository.getByID(id).locked = true;
repository.save();
//unlock here
return id;
}
}
I had a similar type of issue that I resolved at the database level:
- assign each worker a unique identifier
- when asking for a record from the coordinator the workers would transmit their identifier
- the DB routine was a stored procedure that started by acquiring an application lock (SQL Server)
- I would then look for a "free" record, then "stamp it" with the worker identifier so we know it's locked and by whom
- return the record to the worker
精彩评论