for .Net 8 you should add package 'Microsoft.Extensions.Hosting.WindowsServices' and add UseWindowsService(). It's activate only if it detects the process is running as a Windows Service.
IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService()
....
.Build();
await host.RunAsync();