Because a service must be run from within the context of the Services Control Manager rather than from within Visual Studio .NET, 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.
因为NT服务必须在服务控制管理器的控制下运行,而不受控于Visual Studio.net,所以调试NT服务就不像调试其它Visual Studio应用程序一样那么简单。当您需要调试NT服务时,必须首先启动该服务,并且在它的运行位置附加一个调试器。然后就能使用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.
警告:除非您非常清楚这个进程是什么,了解附加debuger以及清除debuger会产生的结果,否则最好不要给该进程附加debuger。例如:如果为WinLogon进程附加debuger,然后停止调试,系统会挂起,这是因为系统不能脱离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.
您只能给运行中的服务附加debuger.调试进程会中断运行中的服务功能;其实它并没有真的停止或暂停服务进程。服务程序正在运行,当您开始调试时,从技术上,它还是在启动状态,但是它的进程被悬挂。
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 .NET to attach to the service process.
给服务进程加载debuger的方法允许您调试大多数的代码,但不是全部;例如:因为在调试时,服务已经开始运行了,所以用这种方法不能调试服务的OnStart方法和Main方法(用于加载服务)中的代码.对付这种问题的唯一方法就是,在您的服务应用程序中创建另外一个仅仅用于辅助调试的临时服务。您需要安装两个服务,然后启动这个"虚拟"服务加载服务进程。一旦这个临时服务启动进程,您就可以使用Visual Studio.NET 中的调试菜单,为服务进程加载debuger了。
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.
加载debuger后,您可以在代码中加入断点,进行调试。一旦退出了用于加载调试器的对话框,您就进入了调试模式。您能使用服务控制管理器启动、停止、暂停以及继续您的服务,用于运行到您设定的断点。您一定要在调试成功后把这个临时服务卸载。
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
The Processes dialog box appears.
Tip The process will have the same name as the executable file for your service.
The Attach to Process dialog box appears.
Note You are now in debug mode.
你可以使用这个链接引用该篇文章 http://publishblog.blogchina.com/blog/tb.b?diaryID=1857046