Building a .NET Core console app with ASP.NET Core web server built in?

Viewed 4018

I see various tutorials online about building a .NET Core console app and building an ASP.NET Core app, but what I don't find is something about building a .NET Core console app that starts with console output but also contains an ASP.NET web app. In this case I am writing an app that runs as a Windows or a Linux service. It runs as a daemon. So I'm now looking to add an ASP.NET Core web interface to output status information.

Is there a recommended way to do this? I presume I need to create a WebHostBuilder and create my new WebHost with .Build(), but should I create a new project for this and reference it from my console app project? If so, which SDK should it use for the project? I can't use Microsoft.NET.Sdk.Web because that requires a Main method which I obviously don't want to have if this is going to be referenced from an existing app that has its own Main method, but if I use Microsoft.NET.Sdk.Razor, I seem to get a bunch of dependency version conflicts and errors like:

Version conflict detected for Microsoft.Extensions.Configuration.Abstractions. Install/reference Microsoft.Extensions.Configuration.Abstractions 2.1.1 directly to project MyProject to resolve this issue.

So I feel like I'm missing something fundamental here; what exactly is the best practice for embedding a web app inside a console app in .NET Core?

1 Answers

You could definitely install the ASP.NET Core packages in your .NET Core project, and build a WebHost which will be used as a web server component in your app, as you said yourself. An easy way to do so is by installing the Microsoft.AspNetCore.App metapackage, which would include all the package dependencies commonly used for ASP.NET Core apps.

As ASP.NET Core technologies are beyond the scope of my answer, I'll be using brief examples from this point on.

After you have installed the Microsoft.AspNetCore.App metapackage in your project, you can proceed to create a Startup class to initialize your server, and develop the app by using ASP.NET Core technologies. A simple Startup class to serve as an example:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        /* configure any services you need here */
    }

    public void Configure(IApplicationBuilder app)
    {
        // Output a "hello world" to the user who accesses the server
        app.Use(async (context, next) =>
        {
            await context.Response.WriteAsync("Hello, world!");
        });
    }
}

Then you can proceed create a web host with something like that:

IWebHost myHost = WebHost.CreateDefaultBuilder()
    .UseStartup<Startup>()
    .Build();

Finally, you must call the Run() method in order for your web host to run and start listening for connections:

myHost.Run();

Run the app and the server will run with some default configs (probably under http://localhost:5000/ address... server's address/port will be print to your console's output by default). Try accessing the server through your browser and it should print a nice "hello world" message to you.

The Run() method is a blocking method. As you already have a console app which you want to "extend" with a web host, you should probably check out the Start() method, which fires the web host without blocking your console app, and you can use this in order to still be able to do things, like taking the user's input. There is a small example of doing that in this doc page.

Related