如何调试服务

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.

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.

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 .NET 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 Installing and Uninstalling Services.
  2. Start your service, either from Services Control Manager, Server Explorer, or from code. For more information, see Starting Services.
  3. In Visual Studio .NET, 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 Starting Services.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值