bpeikes
bpeikes

Reputation: 3705

UseWindowsService not working to start service

Have a .Net Core 5.0 API app that I’m trying to deploy as a Windows Service.

I’ve added UseWindowsService(), with CreateDefaultBuilder, and am able to build and publish.

I then register the service with sc create. All seems to be fine at this point.

When I try starting the service though, I get an error stating that the service hasnt responded in a timely manner.

I thought that adding the call to UseWindowsService should be enough, but I’ve seen other examples with Main being an async Task function instead of void.

Does Main have to be async to run as a service? If not, why wont my application run as a service properly?

Upvotes: 1

Views: 2119

Answers (1)

mcbr
mcbr

Reputation: 781

Check if the application is throwing an exception when the service is starting, something like this:

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            CreateHostBuilder(args).Build().Run();
        }
        catch (Exception ex)
        {
            File.WriteAllText("some/folder/start-error.log", ex.ToString());
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            })
            .UseWindowsService();
}

Upvotes: 3

Related Questions