Share via


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.

Warning

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.

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

  1. Install your service. For more information, see How to: Install and Uninstall Services.

  2. Start your service, either from Services Control Manager, Server Explorer, or from code. For more information, see How to: Start Services.

  3. In Visual Studio, choose Processes from the Debug menu.

    The Processes dialog box appears.

  4. Click Show system processes.

  5. 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.

    The Attach to Process dialog box appears.

  6. Choose any appropriate options, and then click OK to close the dialog box.

    Note

    You are now in debug mode.

  7. Set any breakpoints you want to use in your code.

  8. 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.

See Also

Tasks

How to: Install and Uninstall Services

How to: Start Services

Concepts

Introduction to Windows Service Applications