.NET Core 3.1 Console App as a Windows Service

I forgot about answering this question as I solved it a few hours later after I asked it, but you can just add ".UseWindowsService()" to the Host.CreateDefaultBuilder(args) line. eg:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseWindowsService()                     //<==== THIS LINE
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.SetMinimumLevel(LogLevel.Trace);
            })
            .UseNLog();

Use IWebHostBuilder instead of IHostBuilder:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((context, config) =>
        {
            // Configure the app here.
        })
        .UseNLog()
        .UseUrls("http://localhost:5001/;" +
                    "https://localhost:5002/;")
        .UseStartup<Startup>();

You also need the following packages:

Microsoft.AspNetCore.Hosting;
Microsoft.AspNetCore.Hosting.WindowsServices;

Modify your main function:

bool isService = !(Debugger.IsAttached || args.Contains("--console"));
var builder = CreateWebHostBuilder(args.Where(arg => arg != "--console").ToArray());
var host = builder.Build();

if (isService)
{
    host.RunAsService();
}
else
{
    host.Run();
}

For installation of the service use the tool sc.exe. You can run the app as console app by passing --console as argument to the app. For debugging you need to pass --console as well.


In my case, I did have a "UseWindowsService()" statement included in my host builder setup. However, I have that configuration broken out across multiple lines and the problem was that, at some point during development, I had ALSO placed a:

UseConsoleLifetime()

statement into the mix further down in the code. Once I figured out what was going on, using the following partial code block resolved the issue:

        var hostBuilder = Host.CreateDefaultBuilder(args);
        if (WindowsServiceHelpers.IsWindowsService())
        {
            hostBuilder.UseWindowsService();
        }
        else
        {
            hostBuilder.UseConsoleLifetime();
        }

Note, WindowsServiceHelpers is a static class in the "Microsoft.Extensions.Hosting.WindowsServices" namespace.