2017-08-03 7 views
0

コントローラが繰り返し呼び出されるWeb APIがあります。私は2つのWebリクエストの間に時間的な遅延を与えたい。誰でもこれを助けてくれますか?Web APIで2つの同時ウェブリクエストの間に時間遅延を与える方法

+0

この関連するSOの質問をチェックアウトしましたか? :https://stackoverflow.com/questions/20817300/how-to-throttle-requests-in-a-web-api(特にhttps://github.com/stefanprodan/WebApiThrottleを使用するためのアドバイス) – AardVark71

答えて

1

コントローラにヒットする前に、すべてのリクエストが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

関連する問題