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

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

问题描述

是否有比通过 Windows 服务控制管理器启动服务然后将调试器附加到线程更简单的方法来单步执行代码?这有点麻烦,我想知道是否有更直接的方法.

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天全站免登陆