调试Windows服务的简便方法 [英] Easier way to debug a Windows service

查看:209
本文介绍了调试Windows服务的简便方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有比通过Windows Service Control Manager启动服务然后将调试器附加到线程更简单的方法来遍历代码?这有点麻烦,我想知道是否有更简单的方法。

Is there an easier way to step through the code than to start the service through the Windows Service Control Manager and then attaching the debugger to the thread? It's kind of cumbersome and I'm wondering if there is a more straightforward approach.

推荐答案

如果我想快速调试服务,我只在其中放入 Debugger.Break()。当到达那条线时,它将使我回到VS。

If I want to quickly debug the service, I just drop in a Debugger.Break() in there. When that line is reached, it will drop me back to VS. Don't forget to remove that line when you are done.

更新:作为 #if DEBUG 编译指示,您还可以使用 Conditional( DEBUG_SERVICE)属性。

UPDATE: As an alternative to #if DEBUG pragmas, you can also use Conditional("DEBUG_SERVICE") attribute.

[Conditional("DEBUG_SERVICE")]
private static void DebugMode()
{
    Debugger.Break();
}

在您的 OnStart 上,只需调用此方法:

On your OnStart, just call this method:

public override void OnStart()
{
     DebugMode();
     /* ... do the rest */
}

那里,代码只会在调试版本期间启用。在进行此操作时,为服务调试创建一个单独的构建配置可能会很有用。

There, the code will only be enabled during Debug builds. While you're at it, it might be useful to create a separate Build Configuration for service debugging.

这篇关于调试Windows服务的简便方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆