Tuesday, June 07, 2005

How to debug the OnStart() method of a service in .NET?

It's a bit trickly to debug the OnStart() method of a service. To debug a service, U need to attach the process to the debugger (that means the process is already started and the OnStart() has been executed). So how do U debug the OnStart() method.

First, the important concept is that U can have more than one service running inside a process i.e. 2 or 3 services can share the same process. (Remember thats why we have 2 classes: ServiceProcessInstaller and ServiceInstaller)
So follow the following steps:
  • Make a dummy service under the same process as Ur main service
  • Start the dummy service and attach the debugger to the process
  • Put a break-point in the OnStart() method of Ur main service.
  • Then start Ur main service... The break point will be hit ....Yahooooo.....