Wednesday, September 2, 2009

Simplest Way to Debug a windows Service

Step 1: Create a windows Service.
Setp 2: Open Program.cs
Step 3: Comment the main function
Step 4: Add the Following in ur main function

static void Main()
#if (!DEBUG) System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);

#else
// Name of ur Service
Service1 service = new Service1();
// The Function you want to call on start
service.XYZ(); System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif
}




Happy Debugging...

Source: Check Full Code here