-
Notifications
You must be signed in to change notification settings - Fork 10.3k
UseRateLimiter API deviates by the Action-based config approach #41655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There's a difference between Sometimes, services are required for middleware to function. In those cases, the In other cases where middleware does not require services to function, we don't want to force people to call an By the time the If you prefer using callbacks to configure your rate limiter options, you can still call builder.Services.Configure<RateLimiterOptions>(options =>
{
options.DefaultRejectionStatusCode = 429;
});
// ...
app.UseRateLimiter(); People concerned about playing nice with DI and combining with other configuration sources and callbacks should configure options before the host and DI container are built. But when you're not worried about playing nice with DI, I think it's cleaner to directly pass in options than to mutate options objects in callbacks: app.UseRateLimiter(new()
{
DefaultRejectionStatusCode = 429
}); This isn't a new pattern either.
Maybe we slipped up somewhere and are inconsistent with this, but I can't think of any |
This one is actually unclear to me because there isn't any obvious consistency for the IApplicationBuilder extension methods on which ones use which approach and the reasoning behind the choice. For example (not exhaustive list):
What's the reasoning behind "cleaner" here? And what makes it unclean in this case but clean in all the other ones outlined above. For reference (and I know that this isn't directly affecting ASPNET Core but it's more of an ecosystem thing) the most popular .NET libraries approach their own middleware or configuration with Actions over raw objects, so more often than not, the current As a consumer, assuming that I own the Ultimately I am looking more of a reasoning behind the choice other than anything so I can apply it to my own code too, especially since the original Rate Limiting API Proposal by @rafikiassumani-msft used an Action over the raw object too. |
@Elfocrash Instead of changing that existing overload, we can have another one with @halter73 I am pretty sure what you said is correct, but I didn't find anywhere that is documented so that people could know what's the recommended and general practices. Again the best place for that documentation would be here https://docs.microsoft.com/en-us/dotnet/core/extensions/options-library-authors |
@ShreyasJejurkar Does it make sense then to change the default public static IApplicationBuilder UseRateLimiter(this IApplicationBuilder app, Action<RateLimiterOptions>? configure = null) and allow people to provide the value if they need it and then just keep the other overload that just uses Or is there a preference towards having all 3 (empty, direct object, action-based)? |
Is there any paramter in options which is mandatory to set to RateLimiter work it properly!? (I don't know as I am yet to explore). If yes then no point of having empty overload, if no, then we should have empty overload to avoid cases like this (#39251) |
The default behavior of the limiter when no custom options are passed is to register a |
Please don't add overloads that vary only by style. Overall, we haven't been consistent about these patterns. We revisit it every few years and seem to come to different conclusions. |
I thought the same some days ago like what's the point of having this many overloads which does the same thing under the hood? But then I thought (considered) if the docs are saying, then there might be something that I don't know!! |
The difference with all these methods is that that those they configuring builders rather than options.
Even If you call I still cannot think of any |
I agree that this is not very usable in its current state. In the PR that introduces the feature (#41008), @wtgodbe commented:
I think we will end up adding a @wtgodbe Do we have any issues filed yet tracking adding endpoint awareness to |
Closing since this is superseded by #41667 |
We're proposing switching to an action-based approach in #42667 |
Background and Motivation
The most common approach when it comes to configuring options/settings in
AddXXX
,UseXXX
andMapXXX
methods during project setup is to use the Action-based approach.For example AddControllers uses
not
Which in return is being invoced with the
services.Configure(configure)
method which registers them as Options.This is pretty standard across most .NET APIs (and libraries).
The UserRateLimiter API is using the object based approach which feels out of place compared to the other ones
Proposed API
Instead of providing the object based API, provide an Action based one instead:
Usage Examples
Risks
We are still in previews so there isn't a breaking change risk.
The text was updated successfully, but these errors were encountered: