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?
            Asked
            
        
        
            Active
            
        
            Viewed 573 times
        
    0
            
            
        - 
                    Have you checked out this related SO question ? : https://stackoverflow.com/questions/20817300/how-to-throttle-requests-in-a-web-api (and especially the advice to use https://github.com/stefanprodan/WebApiThrottle) – AardVark71 Aug 03 '17 at 06:07
 
1 Answers
1
            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
        Gurwinder Singh
        
- 214
 - 1
 - 12