Alberto said:
Try running the service attached to the debugger, so that you can
trace execution step by step and see where it is failing. To do this,
start the service, and then from Visual Studio select Debug -> Attach to
Process, and select the service that you started. In this way you can
use the debugger against the service in the same way as if you were
debugging a standard windows executable.
This works, but a drawback of this method is that it doesn't allow you to
debug the process from the start, so you can miss events you need to see.
It's also quite inconvenient to have to do this every time.
A more convenient way of debugging a service is to run it as a console
application instead. To do this, change the boilerplate code generated for
the Main() method to something like this:
Service1 service = new Service1();
if (Environment.UserInteractive) {
service.OnStart();
Console.ReadLine();
service.OnStop();
} else {
ServiceBase.Run(service);
}
You will now be able to debug the service from within Visual Studio. Be
aware that there are differences between running the application like this
and running it as a service, the most important being that services
typically run under the local system account rather than your user account.
This method will not help you diagnose failures that are a result of the
service running in a special environment, but it will help you find
"regular" bugs.
Also, to diagnose unexpected exits, it always pays to hook up an event
handler to AppDomain.UnhandledException and write verbose logging somewhere.
Apropros logging: this, too, is invaluable for services, since the event log
should be used sparingly and is not suitable for dumping debugging
information to. I recommend a library like log4net for this.