Many APIs, such as the GitHub ' s API, have traffic control practices. Use rate limiting to prevent the client from making too many requests to your API in a short amount of time. For example, we can limit the anonymous API client to a maximum of 60 requests per hour, and we can make more requests from more authenticated clients. So how does ASP. NET Webapi implement such a function? There is an implementation above the project Webapicontrib: https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib/ Messagehandlers/throttlinghandler.cs, with good scalability.
The simplest approach is to use Throttlinghandler to register with simple parameters, such as controlling 60 requests per user every hour:
Config. Messagehandlers.add (Newthrottlinghandler , New Inmemorythrottlestore (), id = Timespan.fromhours (1)));
The Ithrottlestore interface uses ID + the current number of requests. Inmemorythrottlestore has only one in-memory storage, but you can easily extend the implementation as a distributed cache or database. You can also easily customize the behavior of the Throttlinghandler, for example, we have better control over an IP address.
Throttling ASP. NET Web API calls
Introducing ASP. NET Web API Throttling Handler
Throttling Suite for Web API
Controlling the ASP. NET Web API Call frequency