Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        }
    }
}
like image 209
WynDiesel Avatar asked Oct 14 '25 07:10

WynDiesel


2 Answers

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.

like image 109
WynDiesel Avatar answered Oct 17 '25 21:10

WynDiesel


There are some points to verify.

  1. Intall Microsoft.Extensions.Hosting.WindowsServices and use it in your project. You can use some log package like Serilog, but it is optional.

enter image description here

  1. Configure the publish with your options. Important to check ReadyToRun and Self-contained.

enter image description here

like image 29
Claudio Camargos Avatar answered Oct 17 '25 19:10

Claudio Camargos