How to use Worker Service in Windows

When we try to create a Worker Service project in Windows, do not forget to change depandency to Microsoft.Extensions.Hosting.WindowsServices

This platform is setting to package IHosting by default.

We need to change the refering package to WindowsServices, then add an new implement in Programs.cs

DO NOT FORGET ADD UseWindowsService() to support framework in windows system.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args).UseWindowsService()
        .ConfigureServices((hostContext, services) =>
        {
           services.AddHostedService();
        });

Leave a Comment