I have an existing .NET Core 3.1 API web application which I'd like to host as a windows service. What is the best way to achieve this?
I've found several tutorials stating that adding the nuget package Microsoft.Extensions.Hosting.WindowsServices and using the UseWindowsService() function should be enough to host it as windows service.
This doesn't work for me. I'll get Error 1053: The service did not respond to the start or control request in a timely fashion.
I've also read about worker services but I have no idea how this would work with an existing API project.
My Program.cs looks like this:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false)
.Build();
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls(config.GetSection("Hosting:Url").Value);
webBuilder.UseStartup<Startup>();
}).UseWindowsService();
}
}