dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.23k stars 9.95k forks source link

RateLimiter is not working if I use IHost/IHostBuilder #50244

Closed emersion-uday closed 1 year ago

emersion-uday commented 1 year ago

Is there an existing issue for this?

Describe the bug

I am trying to implement RateLimiter in my code but I see RateLimiter did not work. After working a couple of days I realised that RateLimiter Extension is not working as expected.

When I use WebApplicationBuilder it works. The following code which uses WebApplicationBuilderworks fine.

           var builder = WebApplication.CreateBuilder(args);

            // Some Code goes here.
            builder.Services.AddRateLimiter(opt =>
            {

                opt.RejectionStatusCode = StatusCodes.Status429TooManyRequests;

                opt.AddFixedWindowLimiter("LimitReuests", options =>
                {
                    options.AutoReplenishment = true;
                    options.PermitLimit = 3;
                    options.Window = TimeSpan.FromSeconds(30);
                });
            });
            var app = builder.Build();
           // Oher Code
            app.UseRateLimiter();
            app.Run();

In the following code where I use IHost/IHostBuilder, RateLimiter did not work:

public class Program
    {
        public static async Task Main(string[] args)
        {
            using IHost host = CreateHostBuilder(args).Build();
            await host.StartAsync();
            await host.WaitForShutdownAsync();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context, builder) =>
                {

                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel(options =>
                    {
                        options.AddServerHeader = false;
                        options.Limits.MaxRequestBodySize = int.MaxValue;
                    });
                    webBuilder.UseStartup<Startup>();
                });
    }

public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddRateLimiter(opt =>
            {

                opt.RejectionStatusCode = StatusCodes.Status429TooManyRequests;

                opt.AddFixedWindowLimiter("LimitReuests", options =>
                {
                    options.AutoReplenishment = true;
                    options.PermitLimit = 3;
                    options.Window = TimeSpan.FromSeconds(30);
                });
            });
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(e=>e.MapControllers());

            app.UseRateLimiter();
        }
    }

Expected Behavior

RateLimiter should work with IHostBuilder and IHost.

Steps To Reproduce

Create a .netcore application where It is using IHostBuilder and IHost as Application Host and apply RateLimiter as shown in above code.

Exceptions (if any)

No response

.NET Version

7.0.307

Anything else?

No response

pinkfloydx33 commented 1 year ago

Doesn't app.UseRateLimiter() need to come before app.UseEndpoints()?

emersion-uday commented 1 year ago

@pinkfloydx33 Thanks for your reply. I was not aware of that. Can you point me to any Documentation which has such details?