C
C#3w ago
rexaliis

Get associated RateLimiter for the current HttpContext instance

Hello! I'm currently using and Microsoft.AspNetCore.RateLimiting and my question is if is there any way to get the associated RateLimiter instance for the current HttpContext.
app.Use((httpContext, next) =>
{
// Apparently, it is not possible to do this through dependency injection.
var rateLimiter = httpContext.RequestServices.GetService<RateLimiter>();
}
app.Use((httpContext, next) =>
{
// Apparently, it is not possible to do this through dependency injection.
var rateLimiter = httpContext.RequestServices.GetService<RateLimiter>();
}
8 Replies
Sehra
Sehra3w ago
it's a middleware which use the configured IOptions<RateLimiterOptions>, what are you trying to do?
rexaliis
rexaliisOP3w ago
I require access to the RateLimiterStatistics of the current request RateLimiter
Sehra
Sehra3w ago
there is not really a current rate limiter, it can be either the global or a combination of them
rexaliis
rexaliisOP3w ago
I would like to send information about the current rate limit in the headers of each request for more dynamic control Okay, then what I probably want is a PartitionedRateLimiter
Sehra
Sehra3w ago
not really possible. at most you can provide a callback for OnRejected, but there are not other extension points at the moment so you can't for example provide remaining quota
Sehra
Sehra3w ago
https://github.com/dotnet/aspnetcore/issues/44140 existing issue, not much has happened
GitHub
Rate limiting middleware - Statistics about rate limiters · Issue #...
Is there an existing issue for this? I have searched the existing issues Is your feature request related to a problem? Please describe the problem. The ASP.NET Core rate limiting middleware is grea...
rexaliis
rexaliisOP3w ago
It’s really a shame, and a bit of a problem for me Thank you so much for the help Sehra!
Sehra
Sehra3w ago
could take a look at https://github.com/stefanprodan/AspNetCoreRateLimit if that will be a better fit
GitHub
GitHub - stefanprodan/AspNetCoreRateLimit: ASP.NET Core rate limiti...
ASP.NET Core rate limiting middleware. Contribute to stefanprodan/AspNetCoreRateLimit development by creating an account on GitHub.

Did you find this page helpful?