FROM:HTTP://MSDN.MICROSOFT.COM/ZH-CN/LIBRARY/7A50SYB3 (vs.80). aspx
Because the service must run from the context of the Service Control Manager, rather than from Visual Studio, debugging services are not as simple as debugging other Visual studio application types. To debug a service, you must first start the service, and then attach a debugger to the process that is running the service. You can then debug your application with all of the standard debugging features of Visual Studio.
Warning |
Do not attach to a process unless you know what the process is, and you know the consequences of attaching to or possibly canceling the process. For example, if you attach to the WinLogon process and then stop debugging, the system pauses because there is no WinLogon and the system cannot run. |
You can only attach the debugger to a running service. The attached process interrupts the current operation of the service; it does not really terminate or suspend the processing of the service. That is, if the service is running when debugging begins, the service is technically still in the "Started" state when debugging, but its processing is suspended.
Processes attached to a service enable you to debug most service code, but not all; for example, because the service is already started, you cannot debug the code in the service's OnStart method in this way, or debug the code in the Main method that loads the service. One way to resolve this problem is to create a temporary service in a service application that is only useful for debugging. You can install all two services, and then start the virtual service to load the service process. Once the temporary service has started the process, you can use the Debug menu in Visual Studio to attach to the service process.
After you attach the process, you can set breakpoints and use these breakpoints to debug your code. When you exit the dialog box that is attached to the process, it is actually in debug mode. You can use the Service Control Manager to start, stop, pause, and resume your service, so hit a breakpoint that has been set. After successful debugging, remove the virtual service.
Attention |
Debugging the OnStart method can be difficult because Windows Service Manager restricts all attempts to start the service within 30 seconds. For more information, see Troubleshooting: Debugging Windows Services. |
Debugging Services
Install your service. For more information, see How to: Install and uninstall services.
You can start the service from the Service Control Manager, Server Explorer, or code. For more information, see How to: Start a service.
In Visual Studio, select Processes from the Debug menu.
The Processes dialog box appears.
Click Show system Processes.
Click the service's process in the available processes area, and then click Attach.
Tips |
The process will have the same name as the service's executable file. |
The Attach to Process dialog box appears.
Select any of the appropriate options, and then click OK to close the dialog box.
Attention |
You are now in debug mode. |
Set any breakpoints that you want to use in your code.
Access the Service Control Manager and manipulate your service, and send a stop, pause, and resume command to hit your breakpoint. For more information about running the Service Control Manager, see how to: Start a service. Please see Task
How to: Install and Uninstall services
How to: Start a service concept
Introduction to Windows Service applications