容易调试一个C#Windows服务方式 [英] Easier way to debug a C# Windows Service

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

问题描述

有没有以逐步code,而不是通过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.

更新:作为一种替代#如果DEBUG编译指示,你也可以使用条件(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 */
}

在那里,code只会调试期间启用的基础之上。虽然你在它,它可能是创建服务调试一个单独的生成配置非常有用。

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

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

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