add the extension Microsoft.Extensions.Hosting.WindowsServices
in Program.cs write:
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.UseWindowsService()
.Build();
await host.RunAsync();
sc create workertest binPath=c:codeworkerpubWorkerTest.exe