DotNetRateLimiter 2.0.0
dotnet add package DotNetRateLimiter --version 2.0.0
NuGet\Install-Package DotNetRateLimiter -Version 2.0.0
<PackageReference Include="DotNetRateLimiter" Version="2.0.0" />
paket add DotNetRateLimiter --version 2.0.0
#r "nuget: DotNetRateLimiter, 2.0.0"
// Install DotNetRateLimiter as a Cake Addin #addin nuget:?package=DotNetRateLimiter&version=2.0.0 // Install DotNetRateLimiter as a Cake Tool #tool nuget:?package=DotNetRateLimiter&version=2.0.0
This is a RateLimit that works with ActionFilters and EndPointFilters. An approach designed to control the request rate for a particular Action or Endpoint. The idea behind this solution is to solve the middleware problem because the Middlewares affects all requests, but here you can limit some of the critical endpoints. see How-To-Use
Rate Limit uses In-Memory cache by default, but if you set up a Redis connection it will use Redis, it is recommended to use Redis for checking the rate limit in the distributed applications. By default, it limits the IP address but you can set ClientId in request headers and the header name is configurable.
How to add in DI
You can add RateLimit in Startup like this:
using DotNet.RateLimiter;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRateLimitService(builder.Configuration);
How to use
You can see the Demo project to know how to use it in all scenarios and also you can follow this article in Medium.
Simple usage
Using RateLimit without any parameters
[HttpGet("")]
[RateLimit(PeriodInSec = 60, Limit = 3)]
public IEnumerable<WeatherForecast> Get()
{
....
}
For MinimalAPI .NET 7+
app.MapGet("/weatherforecast", () =>
{
return Results.Ok();
})
.WithRateLimiter(options =>
{
options.PeriodInSec = 60;
options.Limit = 3;
});
Using Route parameters
[HttpGet("by-route/{id}")]
[RateLimit(PeriodInSec = 60, Limit = 3, RouteParams = "id")]
public IEnumerable<WeatherForecast> Get(int id)
{
....
}
For MinimalAPI .NET 7+
app.MapGet("/weatherforecast/{id}", (int id) =>
{
return Results.Ok();
})
.WithRateLimiter(options =>
{
options.PeriodInSec = 60;
options.Limit = 3;
options.RouteParams = "id";
});
Using Query parameters
[HttpGet("by-query/{id}")]
[RateLimit(PeriodInSec = 60, Limit = 3, RouteParams = "id", QueryParams = "name,family")]
public IEnumerable<WeatherForecast> Get(int id, string name, [FromQuery] List<string> family)
{
....
}
For MinimalAPI .NET 7+
app.MapGet("/weatherforecast/{id}", (int id, string name, string family) =>
{
return Results.Ok();
})
.WithRateLimiter(options =>
{
options.PeriodInSec = 60;
options.Limit = 2;
options.QueryParams = "name,family";
options.RouteParams = "id";
});
Using with Body parameters (Only for ActionFilters)
// body parameter only works on root parameters and does not work on nested parameters.
[HttpPut]
[RateLimit(PeriodInSec = 60, Limit = 3, BodyParams = "temperatureC" )]
public IActionResult Update([FromBody] WeatherForecast weatherForecast)
{
....
}
....
public class WeatherForecast
{
public int TemperatureC { get; set; }
}
For more details please check the GitHub README in the repo.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETCoreApp 3.1
- AsyncKeyedLock (>= 6.3.4)
- Microsoft.Extensions.Caching.Memory (>= 3.1.0)
- Microsoft.Extensions.Hosting (>= 3.1.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.10)
-
.NETStandard 2.0
- AsyncKeyedLock (>= 6.3.4)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Caching.Memory (>= 3.1.0)
- Microsoft.Extensions.Hosting (>= 3.1.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.10)
-
.NETStandard 2.1
- AsyncKeyedLock (>= 6.3.4)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Caching.Memory (>= 6.0.0)
- Microsoft.Extensions.Hosting (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.10)
-
net5.0
- AsyncKeyedLock (>= 6.3.4)
- Microsoft.Extensions.Caching.Memory (>= 5.0.0)
- Microsoft.Extensions.Hosting (>= 5.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.10)
-
net6.0
- AsyncKeyedLock (>= 6.3.4)
- Microsoft.Extensions.Caching.Memory (>= 6.0.0)
- Microsoft.Extensions.Hosting (>= 6.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.10)
-
net7.0
- AsyncKeyedLock (>= 6.3.4)
- Microsoft.Extensions.Caching.Memory (>= 7.0.0)
- Microsoft.Extensions.Hosting (>= 7.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.10)
-
net8.0
- AsyncKeyedLock (>= 6.3.4)
- Microsoft.Extensions.Caching.Memory (>= 8.0.0)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.3)
- RedLock.net (>= 2.3.2)
- StackExchange.Redis (>= 2.7.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on DotNetRateLimiter:
Repository | Stars |
---|---|
sa-es-ir/DotNet.RateLimit
A Distributed RateLimit for Controller-Actions and Minimal API.
|