How to: Debug Windows Service Applications in C#
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.Caution |
---|
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. |
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.
Note |
---|
Debugging the OnStart
method can be difficult because the Windows Service Manager imposes a
30-second limit on all attempts to start a service. For more
information, see Troubleshooting: Debugging Windows Services. |
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.
Tip The process will have the same name as the executable file for your service.
- Choose any appropriate options, and then click OK to close the dialog box.
Note 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
0 comments:
Post a Comment