Lock Web API controller method

后端 未结 2 911
孤独总比滥情好
孤独总比滥情好 2021-02-13 10:38

I\'m developing an ASP.NET Web Api application with C# and .Net Framework 4.7.

I have a method in a controller that I want to execute only by one thread at a time. In ot

2条回答
  •  你的背包
    2021-02-13 11:27

    I guess you can lock on different levels, with your approach being one.

    I've come across a system (or a web app) that utilizes redis as an external service. In redis we save a key for the request, in your case I guess it could be the name of the method. With this approach we first have an action filter that checks if a lock exists (talking to redis) and then blocks the request.

    The good thing with redis is that it's very fast, and let's your specify a timeout at which the key will dissapear. This prevents from being stuck with the lock forever.

提交回复
热议问题