Search code examples
c#console-application.net-core

Startup.cs in a self-hosted .NET Core Console Application


I have a self-hosted .NET Core Console Application.

The web shows examples for ASP.NET Core but I do not have a web server. Just a simple command line application.

Is it possible to do something like this for console applications?

public static void Main(string[] args)
{
    // I don't want a WebHostBuilder. Just a command line

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

I would like to use a Startup.cs like in ASP.NET Core but on console.

How do I to this?


Solution

  • All .NET Core applications are composed of well-crafted independent libraries and packages which you're free to reference and use in any type of application. It just so happens that an ASP.NET Core application comes preconfigured to reference a lot of those libraries and exposes an http endpoint.

    But if it's Dependency Injection you need for your console app, simply reference the appropriate library. Here's a guide: https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/