8

The service could not be started. The service did not report an error.

I encounter this error whenever I install a windows service project in the command line. It's true that there's an error in my code, but how can I find that error with this kind of error message?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Lance
  • 2,201
  • 3
  • 31
  • 47

3 Answers3

9

There is an exception in your service's OnStart() method, add

 try{...} 
 catch(Exception ex)
 {
     //write to file ex.ToString();
 }

and log your exception to file

Arsen Mkrtchyan
  • 47,086
  • 29
  • 143
  • 178
  • 3
    Use windows event log... EventLog.WriteEntry(String.Format("WcfServiceHostTest \n Exception Message: {0}\nTrace: {1}", ex.Message, ex.StackTrace), EventLogEntryType.Error); – Cruiser KID May 21 '15 at 08:01
2

Add error handling block (catching UnhandledException or just try/catch block around suspected code) and log it (I use either Trace or Debug - you can view that messages with DebugView).

In order to give idea to Service Manager that there is error (just to help user) you can:

service.ExitCode = 1064; //ERROR_EXCEPTION_IN_SERVICE - just example

Where "service" is your Service's object.

Josip Medved
  • 3,429
  • 22
  • 33
  • This ExitCode will appear both in `net start` output, or starting it from the Services MMC window. – StuartN Nov 10 '17 at 09:32
0

If you have Visual Studio installed and you're using .NET, call System.Diagnostics.Debugger.Break() in your OnStart() function. When your service starts, you'll be prompted to debug the service. Select the Visual Studio option and you'll break into the debugger where the programmatic breakpoint is. You can debug normally from there.

Matt Davis
  • 43,149
  • 15
  • 89
  • 118