15

I have a Windows Service that I am trying to debug. Now it fails to start even though the current code used to work. The error is:

Windows could not start the MyService service on Local Computer

Error 1053: The service did not respond to the start or control request in a timely fashion.

To isolate the error, I tried to comment out everything. The main method looks like this:

TextWriter tt = new StreamWriter(@"C:\startup.text", true);
tt.WriteLine("Starting up the service");
tt.Close();

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
   { 
       new MyService()
   };

TextWriter tt2 = new StreamWriter(@"C:\startup.text", true);
tt2.WriteLine("Run...");
tt2.Close();

It prints out both "Starting up the service" and "Run..." to the log file. I also stripped the inside of MyService so it's empty. There is a try/catch around any code, which now is reduced to some log lines like above. I never enters the catch statement, which would have logged it.

Everything in OnStart has been commented out:

protected override void OnStart(string[] args)
{
}

So I'm basically out of ideas. I thought the error was because the Start method never finishes (or doesn't within 30 seconds). Is there some other method that is called? Any ideas are appreciated.

Extra info: The constructor in MyService is empty. If I insert some Thread.Sleep(5000) lines, then it takes longer beofre the error message about Error 1053 pops up. The Main method seems to have to exit (without error).

Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106
  • Make sure you catch all the unhandled exceptions and write them to your log. For example: http://stackoverflow.com/questions/58280/unhandledexception-handler-in-a-net-windows-service ... that might give you some more information on why it is failing. – Christophe Geers Sep 13 '11 at 13:23

3 Answers3

14

You are missing ServiceBase.Run call:

ServiceBase[] servicesToRun = new ServiceBase[]
                                { 
                                    new MyService() 
                                };
ServiceBase.Run(servicesToRun);

It might also be a good idea to subscribe to unhandled exceptions notification:

static void Main() {
    ...
    AppDomain.CurrentDomain.UnhandledException 
                                      += CurrentDomain_UnhandledException;
    ...
}

private static void CurrentDomain_UnhandledException(
                                                 Object sender, 
                                                 UnhandledExceptionEventArgs e) {

    if (e != null && e.ExceptionObject != null) {
        // log exception:
    }
}

And add following try/catch to OnStart because .NET/SCM swallows exceptions:

protected override void OnStart(String[] args) {
    try {

    } catch(Exception e) {
        // log exception:
        throw;
    }
}
Dmitry
  • 17,078
  • 2
  • 44
  • 70
  • 1
    Also if you want to debug service initialisation you can put a call to System.Diagnostics.Debugger.Break() in the initialisation code. When this line is reached Windows will ask you if you want to launch the debugger, which will enable you to debug the service from that point. – Polyfun Sep 13 '11 at 13:44
  • 1
    Thank you. The ServiceBase.Run(servicesToRun); line was commented out in the process. – Kasper Hansen Sep 13 '11 at 13:56
  • 1
    For me, it was the debug conditional set to run without calling `ServiceBase.Run(serviceToRun);` – absynce Feb 12 '13 at 23:25
  • This worked in my case, helped my team mate who was about to go MAD. Thanks A LOT!!! – Aimal Khan Sep 13 '17 at 15:48
1

Although this thread is rather old, here my additional answer, as it might help other save time where I put some hours into:

Situation

  • Windows service written in C# that monitors a directory and posts parsed message from file to MQTT topic runs fine on Win10 and also during short tests on WinXP at home

  • In production environment, service runs on Win10 fine, on WinXP crashes after some hours and cannot be restarted (Could not start the filemqttservice service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion)

Solution

Clear event log history! (Doh!) My service was too chatty to fill the event log buffer in some hours. After clearing the event log, the service started again. Of course, I removed all the lines producing messages from the service

Thomas Bobek
  • 126
  • 1
  • 8
0

Just check the .net framework on both side it should be same or the installation side the farmework should be hihger than the develop