Search code examples
c#.net-coreservicekestrel

Service not starting up


This is not a duplicate of this post, as I already attempted the recommended solutions in there.

I've been trying to start my C#/.netCore 3.0/Kestrel as a service.

After struggling to get my app to start as a service, I followed these instructions to create a new exe, with the same result.

After publishing, and adding the service, and trying to start the service from either cmd, powershell, or the service interface, I am prompted with the following 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.

On investigating the system event log, I only see a generic :

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.

When I run the exe/console, it starts up fine without any problems.

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);
        }
    }
}

Solution

  • The issue was that Visual Studio was not set to deploy the service as self-contained.

    After setting this in the project's properties, everything worked fine.