Reputation: 85
I have a web API in which a controller is getting called repeatedly. I want to give a time delay between two web requests. Can anyone please help on this?
Upvotes: 0
Views: 678
Reputation: 214
Before hitting the controller, every request made hits the ActionFilterAttribute. You can create a class which is inheriting from ActionFilterAttribute.
public class AuditTrails : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext filterContext)
{
//Give the request a delay time here, For this you have to filter the request if it is hitting the same controller.
//You can refer this also https://stackoverflow.com/questions/20817300/how-to-throttle-requests-in-a-web-api
}
}
and then use [AuditTrails] at the class level of controller.
[AuditTrails]
public class Controller{}
If you are want to use thread-safety or you want to access a resource for a particular thread and lock it, then the best option is to use lock . For locking, you can refer this https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement
Upvotes: 1