I need to be able to lock certain set of codes in an MVC action depending on the value of a variable
Below code will lock the entire action
public ProductsController : Controller
{
private static object Lock = new object();
public ActionResult MyAction(int id)
{
lock (Lock)
{
// do my work
}
}
}
But I want to allow concurrent threads to be run parallel when value of the 'id' changes
For example lets assume above controller receives below four requests at the same time
request A : id=1
request B : id=1
request C : id=2
request D : id=2
with above code these four requests will be processed one after another
But I want (A,B) and (C,D) to run in parallel like below
request A & B need to wait till one of them finish their work to get processed
request C & D need to wait till one of them finish their work to get processed
Is this achievable in an MVC environment?