• ASP.NET Core,  Microsoft

    Using Kestrel for ASP Core Apps

    As I was looking about with some of the Core 2.2 notes, I came across some interesting notes on how to tweak about with Kestrel settings….

    I’m not sure I like putting Kestrel options up the in the Main Program builder. I tend to prefer these to collect in the startup class, but I get point of why Kestrel needs to be handled close to the Builder (it is after all the html service handling web calls).

    Shall we begin…
    So up in program.cs we do our normal Web Host Builder:

    public class Program
    {
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();
    
            MigrateDatabase(host);
    
            host.Run();
            //CreateWebHostBuilder(args).Build().Run();
        }
    
        private static void MigrateDatabase(IWebHost host)
        {
            using (var scope = host.Services.CreateScope())
            {
                var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
                db.Database.Migrate();
            }
        }
    
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

    This tends to be my way of doing Program.cs. Note I split out the Build part into a static method (the normal default way is to have one fluent state that does a build and run in single line- it’s that commented line in the Main method) 

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup$lt;Startup>()
                    .ConfigureKestrel(opts =>
                    {
                        opts.Limits.MaxConcurrentConnections = 100;
    
                    });

     In my example, the method I push Kestrel setting to would of course have the .ConfigureKestrel with it’s options settings… 

    Check it out… lots of interesting tweaks on settings…

    You will find some details here on MS DOCS:

    Learn about Kestrel, the cross-platform web server for ASP.NET Core.