Skip to content
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

[Breaking change]: ASP.NET Core rate limiting middleware now requires AddRateLimiter #506

Open
1 of 3 tasks
JamesNK opened this issue May 3, 2023 · 0 comments
Open
1 of 3 tasks
Labels
8.0.0 Breaking change Documented The breaking change has been published to the .NET Core docs

Comments

@JamesNK
Copy link
Member

JamesNK commented May 3, 2023

Description

ASP.NET Core rate limiting middleware is being updated in .NET 8 with extra functionality. The middleware now requires services registered with AddRateLimiter.

Version

.NET 8 Preview 5

Previous behavior

Previously, rate limiting could be used without AddRateLimiter. For example, the middleware could be configured by calling Configure<RateLimiterOptions>(o => { }):

var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<RateLimiterOptions>(o => o
    .AddFixedWindowLimiter(policyName: "fixed", options =>
    {
        // configuration
    }));

var app = builder.Build();
app.UseRateLimiter();
app.MapGet("/", () => Results.Ok($"Hello world")).RequireRateLimiting("fixed");
app.Run();

New behavior

If AddRateLimiter is not called on app startup, then ASP.NET Core will throw an informative error:

Unable to find the required services. Please add all the required services by calling 'IServiceCollection.AddRateLimiter' in the application startup code.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
  • Source incompatible: When recompiled using the new SDK or component or to target the new runtime, existing source code may require source changes to compile successfully.
  • Behavioral change: Existing binaries may behave differently at run time.

Reason for change

Rate limiting middleware requires services that are only registered by calling AddRateLimiter.

Recommended action

Ensure AddRateLimiter() is called at application startup.

For example, update Configure<RateLimiterOptions>(o => { }) to use AddRateLimiter():

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRateLimiter(o => o
    .AddFixedWindowLimiter(policyName: "fixed", options =>
    {
        // configuration
    }));

var app = builder.Build();
app.UseRateLimiter();
app.MapGet("/", () => Results.Ok($"Hello world")).RequireRateLimiting("fixed");
app.Run();

Affected APIs

UseRateLimiter()

@ghost ghost locked as resolved and limited conversation to collaborators May 3, 2023
@gewarren gewarren added the Documented The breaking change has been published to the .NET Core docs label May 31, 2023
@guardrex guardrex added the 8.0.0 label Nov 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
8.0.0 Breaking change Documented The breaking change has been published to the .NET Core docs
Projects
None yet
Development

No branches or pull requests

3 participants