Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Kestrel Server Options in .NET 6 Startup

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.

like image 819
brando Avatar asked Jan 25 '26 15:01

brando


1 Answers

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;
});
like image 52
Guru Stron Avatar answered Jan 28 '26 04:01

Guru Stron