Debugging
Finally, when developing a Windows service, it is important to know how to debug it. Unfortunately, you cannot simply set a break point in the Visual Studio .NET IDE and then hit F5 to start the service. Instead, you must first install the service and start it from the Services MMC or from the Server Explorer in VS .NET. Then, do the following:
Set a break point in a location other than the OnStart event (because the service is already started). A good place would be in the event handler for the timer if using a poling mechanism or in the event that handles the notification from MSMQ or the FileSystemWatcher class.
Click on the Debug/Processes menu. This invokes the Processes dialog box that shows all of the running process on the machine, as shown in Figure 1.
Figure 1 The Processes dialog box.
In the dialog box, click on Show System Processes in order to show the Windows services.
Find the service process in the list and click Attach.
Select Common Language Runtime in the resulting dialog box and click OK.
Click Close.
Wait until the next OnTimer event fires (or the service is notified) and VS .NET will immediately go into break mode. From this point, you can step through the execution of the service class.
This dialog box is invoked from the Debug menu and can be used to attach to a running Windows service.