Search code examples
c#windows.net-coreservice

Error 1053: Service did not respond C# .Net Core 3.1


I am trying to start my own C# service as a Windows service. I installed my service using sc.exe create. I can see and modify the Service in the Services Application, but when I try to start, I am always getting the Error 1053. In my Program I am starting a new Thread which runs an infinite while loop in Execute.

static void Main(string[] args) {
    Console.WriteLine("Starting Thread");

    mainThread = new Thread(new ThreadStart(Execute));
    mainThread.Start();

    Console.WriteLine("Thread Started!");
}

public static void Execute() {
    //connect to Database

    while(true) { //while connection open
        ...
    }
}

When I run my Program manually, in a Console, Powershell and in Visual Studio, the Program runs as expected. When I try to run it via Services I get the error. I'm also getting the error when I'm running a empty Main (Main only with Console.WriteLine). I googled really much and tried extending the Timeout in Registry(ServicesPipeTimeout), Using Threads, Installing Core Framework, Reinstalling the service and being owner of the service.

EDIT: I changed everything to run as a Hosted Service. In my Main i have

await new HostBuilder()
            .ConfigureServices((hostContext, services) => { services.AddHostedService<Worker>(); })
            .Build()
            .RunAsync();

But i Still can't run it as a Windows Service, I'm getting the same Error.

I am out of Ideas, any help is highly appreciated.

Regards


Solution

  • I ended up writing it as a Windows Service. I simply inherited ServiceBase and wrote Start and Stop methods. The start method starts a Timer which calls a Method with the infinite loop every minute. See: Error 1053: Service did not respond in time