Search code examples
c#.net-coretimeoutwindows-servicesvisual-studio-2022

Windows Service Not starting fails with Error 1053 (The service did not respond to the start or control request in a timely fashion.)


I use windows and Visual studio (2022) coding in c#, to create a windows service using .Net core the code is generated by visual studio and build by it. it does creates two files, one called Program.cs that have this content

IHost host = Host.CreateDefaultBuilder(args)                 
    .ConfigureServices(services =>
    {
        services.AddHostedService< Worker>();
    })
    .Build();

await host.RunAsync();

and Worker.cs that i have modified to do the minimum but even the original fails to start

public class Worker : BackgroundService

    {
        static int i = 0;
        private readonly ILogger<Worker> _logger;

        private void writefile(string  textf)
        {
            string path = $"C:\\Temp\\{textf}.txt";

            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(textf);
            }
        }
        public Worker(ILogger<Worker> logger)
        {
            writefile("Worker");
            _logger = logger;
        }
        public override Task StartAsync(CancellationToken cancellationToken)
        {
            writefile("StartAsync");

            return base.StartAsync(cancellationToken);
        }
        public override Task StopAsync(CancellationToken cancellationToken)
        {

            writefile("StopAsync");

            return base.StopAsync(cancellationToken);
        }

        //ExecuteAsync is left empty
        protected override  Task ExecuteAsync(CancellationToken stoppingToken)
        {
            writefile($"ExecuteAsync{i++}");
            return Task.CompletedTask;
        }
}

The code above does create the intended files (and i have added this code to track the issue)

When code is Build I install the service using the command prompt sc create MyService binPath="c:\Visual Studio\bin\debug\myservice.exe"

the directory where the .exe resides have all the .dlls , I also tried with the Release version with same error.

The event viewer do not show any other information but the same error.

I have tried also to use my own account as startup account for the service with same result

I have also tried to change the timeout (registry) for services (\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ServicePipeTimeout & \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\myservice\StartTimeout) to a bigger value , but they are just ignored and 30 seconds is always used.

Any ideas ? thanks


Solution

  • // this the solution that worked for me, on the Worker.cs a call was  missing     
    
       IHost host = Host.CreateDefaultBuilder(args)                 
        .ConfigureServices(services =>
        {
            services.AddHostedService< Worker>();
        })
        .UseWindowsService()  // this code is required
        .Build();
        
        await host.RunAsync();