Service won't start

I’m attempting to start a C#/.netCore 3.0/Kestrel service but it fails with an error message “The service did not respond to the start or control request in a timely fashion”. I’ve followed these instructions to create a new exe, but it still doesn’t work. I’m able to start the exe/console without any issues.

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging(
                options => options.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Information))
            .ConfigureServices((hostContext, services) =>
            {
                services.AddHostedService<Worker>()
                    .Configure<EventLogSettings>(config =>
                    {
                        config.LogName = "Sample Service";
                        config.SourceName = "Sample Service Source";
                    });
            }).UseWindowsService();
}


public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;

    public Worker(ILogger<Worker> logger)
    {
        _logger = logger;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
            await Task.Delay(1000, stoppingToken);
        }
    }
}

I am unable to start a C#/.netCore 3.0/Kestrel service as I get the following error message:

Windows could not start the SomeWorker service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion.

The system event log reads “The myWorker service failed to start due to the following error: The service did not respond to the start or control request in a timely fashion.”

I have followed these instructions to create a new exe, but I still get the same result. The exe/console starts up without any issues.

The issue is that the service is not responding to the start or control request within the expected timeframe. One possible solution is to increase the timeout for the service to start by adding the following code to the CreateHostBuilder method:

.UseWindowsService(options =>
{
    options.StartupTimeout = TimeSpan.FromSeconds(30);
})

This will give the service 30 seconds to start up before timing out. You can adjust the timeout as needed.

Here’s the updated CreateHostBuilder method:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(
            options => options.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Information))
        .ConfigureServices((hostContext, services) =>
        {
            services.AddHostedService<Worker>()
                .Configure<EventLogSettings>(config =>
                {
                    config.LogName = "Sample Service";
                    config.SourceName = "Sample Service Source";
                });
        })
        .UseWindowsService(options =>
        {
            options.StartupTimeout = TimeSpan.FromSeconds(30);
        });