0
我有一个Web API,其中一个控制器被重复调用。我想给两个Web请求之间的时间延迟。任何人都可以请帮忙吗?如何在Web API中的两个并发Web请求之间给出时间延迟
我有一个Web API,其中一个控制器被重复调用。我想给两个Web请求之间的时间延迟。任何人都可以请帮忙吗?如何在Web API中的两个并发Web请求之间给出时间延迟
在击中控制器之前,每次发出的请求都会触发ActionFilterAttribute。你可以创建一个继承自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
}
}
然后在控制器的级别级别使用[AuditTrails]。
[AuditTrails]
public class Controller{}
如果你想使用线程安全,或者你要访问的资源用于特定线程和锁定,那么最好的选择是使用锁。对于锁定,你可以参考这个 https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement
你检查了这个相关的SO问题吗? :https://stackoverflow.com/questions/20817300/how-to-throttle-requests-in-a-web-api(特别是使用https://github.com/stefanprodan/WebApiThrottle的建议) – AardVark71