How to throttle requests in a Web Api?

后端 未结 7 526
深忆病人
深忆病人 2020-11-30 17:45

I\'m trying to implement request throttling via the following:

Best way to implement request throttling in ASP.NET MVC?

I\'ve pulled that code into my solut

7条回答
  •  囚心锁ツ
    2020-11-30 18:25

    I am using ThrottleAttribute to limit the calling rate of my short-message sending API, but I found it not working sometimes. API may been called many times until the throttle logic works, finally I am using System.Web.Caching.MemoryCache instead of HttpRuntime.Cache and the problem seems to solved.

    if (MemoryCache.Default[key] == null)
    {
        MemoryCache.Default.Set(key, true, DateTime.Now.AddSeconds(Seconds));
        allowExecute = true;
    }
    

提交回复
热议问题