แก้ไข

แชร์ผ่าน


ConcurrencyLimiterMiddleware is obsolete

ConcurrencyLimiterMiddleware and its associated methods and types have been marked as obsolete.

If you require rate-limiting capabilities, switch to the newer and more capable rate-limiting middleware that was introduced in .NET 7 (for example, RateLimiterApplicationBuilderExtensions.UseRateLimiter). The .NET 7 rate-limiting API includes a concurrency limiter and several other rate-limiting algorithms that you can apply to your application.

Version introduced

ASP.NET Core 8.0 Preview 4

Previous behavior

Developers could use ConcurrencyLimiterMiddleware to control concurrency by adding a policy to dependency injection (DI) and enabling the middleware:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddStackPolicy<options => {
    options.MaxConcurrentRequests = 2;
    options.RequestQueueLimit = 25;
    });

var app = builder.Build();
app.UseConcurrencyLimiter();
// Map endpoints.
app.Run();

New behavior

If you use the Affected APIs in your code, you'll get warning CS0618 at compile time.

Type of breaking change

This change affects source compatibility.

Reason for change

ConcurrencyLimiterMiddleware is infrequently used and undocumented. The newer rate-limiting API has more extensive functionality.

If you're using the older ConcurrencyLimiterMiddleware, we recommend moving to the newer rate-limiting middleware. Here's an example usage of the newer API, RateLimiterApplicationBuilderExtensions.UseRateLimiter:

using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseRateLimiter(new RateLimiterOptions()
    .AddConcurrencyLimiter("only-one-at-a-time-stacked", (options) =>
    {
        options.PermitLimit = 2;
        options.QueueLimit = 25;
        options.QueueProcessingOrder = QueueProcessingOrder.NewestFirst;
    }));

app.MapGet("/", async () =>
{
    await Task.Delay(10000);
    return "Hello World";
}).RequireRateLimiting("only-one-at-a-time-stacked");

app.Run();

Affected APIs

See also