Home > database >  Configuring Kestrel Server Options in .NET 6 Startup
Configuring Kestrel Server Options in .NET 6 Startup

Time:11-10

I am migrating a WebApi from .net5 to .net6. It's going fairly well, but have run into the problem of how to configure Kestrel during startup. The following code is from the Main method of the Program.cs file:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddVariousStuff();
builder.Host
.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder.ConfigureKestrel(serverOptions =>
    {
        serverOptions.Limits.MaxConcurrentConnections = 100;
        serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
        serverOptions.Limits.MaxRequestBodySize = 52428800;

    });


});
var app = builder.Build();
app.UseStuffEtc();
app.Run();

The app startup crashes with the following exception:

System.NotSupportedException: ConfigureWebHost() is not supported by WebApplicationBuilder.Host. Use the WebApplication returned by WebApplicationBuilder.Build() instead.

If I remove anything related to ConfigureWebHostDefaults, then app starts no problem. Am unable to figure out how roll with the new .net6 Kestrel server startup config.

CodePudding user response:

The migration guide's code examples cover that. You should use UseKestrel on the builder's WebHost:

builder.WebHost.UseKestrel(so =>
{
    so.Limits.MaxConcurrentConnections = 100;
    so.Limits.MaxConcurrentUpgradedConnections = 100;
    so.Limits.MaxRequestBodySize = 52428800;
});
  • Related