|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Debug Windows Service Applications
Because a service must be run from within the context of the Services Control Manager rather than from within Visual Studio, debugging a service is not as straightforward as debugging other Visual Studio application types. To debug a service, you must start the service and then attach a debugger to the process in which it is running. You can then debug your application using all of the standard debugging functionality of Visual Studio.
You should not attach to a process unless you know what the process is and understand the consequences of attaching to and possibly killing that process. For example, if you attach to the WinLogon process and then stop debugging, the system will halt because it cannot operate without WinLogon.
You can only attach the debugger to a running service. The attachment process interrupts the current functioning of your service; it does not actually stop or pause the service's processing. That is, if your service is running when you begin debugging, it is still technically in the Started state as you debug it, but its processing has been suspended.
Attaching to the service's process allows you to debug most but not all of the service's code; for example, because the service has already been started, you cannot debug the code in the service's OnStart method this way, or the code in the Main method that is used to load the service. One way to work around this is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this "dummy" service to load the service process. Once the temporary service has started the process, you can then use the Debug menu in Visual Studio to attach to the service process.
After attaching to the process, you can set breakpoints and use these to debug your code. Once you exit the dialog box you use to attach to the process, you are effectively in debug mode. You can use the Services Control Manager to start, stop, pause and continue your service, thus hitting the breakpoints you've set. You would later remove this dummy service after debugging is successful.
To debug a service
Install your service. For more information, see How to: Install and Uninstall Services.
Start your service, either from Services Control Manager, Server Explorer, or from code. For more information, see How to: Start Services.
In Visual Studio, choose Processes from the Debug menu.
The Processes dialog box appears.
Click Show system processes.
In the Available Processes section, click the process for your service, and then click Attach.
The process will have the same name as the executable file for your service.
The Attach to Process dialog box appears.
Choose any appropriate options, and then click OK to close the dialog box.
You are now in debug mode.
Set any breakpoints you want to use in your code.
Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints. For more information on running the Services Control Manager, see How to: Start Services.