Search code examples
.netasp.net-corewebserverkestrel-http-server

Is there a way to run Kestrel (or something similar) dynamically and without the use of host builders?


I would like to spin up web servers whenever I like, from whichever .NET6 process I like.

For example, I would like to start up one or more web servers, at any given moment, from a WPF app, or from a .NET6 service, or from a Blazor app, or from a console application.

The way Microsoft has implemented Kestrel integration makes it EXTREMELY difficult and inflexible for things like this. I am sure there are performance reasons for their current design (optimal integration with IIS, etc), but often you don't care about performance at all (e.g. you want to provide a browser-based management UI for a service, or something like that).

Is there a way to spin up Kestrel web servers in a dynamic and flexible manner? All the examples I see are based on a host builder, that assumes a particular hosting model.

If there is no way to use Kestrel for this, are there any other mature alternatives?

Update:

I have just tried the Microsoft.AspNetCore.Server.Kestrel package and it has some interesting classes like:

new Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer(...);

I might try to use this somehow... Also found this interesting project: https://github.com/mark-cordell/bare-bones-kestrel-server It should provide some ideas and places to start...


Solution

  • You should be able to put this piece of code in any .NET application by adding a <FrameworkReference Include="Microsoft.AspNetCore.App" /> dependency:

    var app = WebApplication.Create()
    app.Map("/", () => "Hello");
    app.Start();
    

    This should allow the usage of these simple APIs in a WPF, Console or whatever application you need. Of course, I'm talking about .NET Core applications, not .NET Framework applications.

    If you're trying to add ASP.NET Core to a generic host based worker service then you can use:

    IHost host = Host.CreateDefaultBuilder(args)
            .ConfigureServices(services =>
            {
                services.AddHostedService<Worker>();
            })
            .ConfigureWebHostDefaults(web =>
            {
                web.Configure(app =>
                {
                    app.UseRouting();
    
                    app.UseEndpoints(e =>
                    {
                        e.MapGet("/", () => "Hello World");
                    });
                });
            })
            .Build();
    

    This will let you add ASP.NET Core to an existing hosted application.

    I have just tried the Microsoft.AspNetCore.Server.Kestrel package and it has some interesting classes like:

    You can do this as well, but:

    • You still need to include the framework reference
    • You need much more boilerplate to wire up the application (for e.g. getting a middleware pipeline with routing is lots of code you need to write by hand).
    • You need to manually wire up all of the dependencies in the constructor which can be a little painful without a DI container.

    The sample you linked to shows just that.