调试窗口服务

时间:2011-08-09 11:36:41

标签: c# debugging windows-services

我想调试窗口服务。我应该在main()中编写什么来启用窗口服务中的调试。我正在使用C#开发窗口服务。

#if(DEBUG)
      System.Diagnostics.Debugger.Break();
      this.OnStart(null);
      System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
 #else
      ServiceBase.Run(this);
 #endif

我写了上面的代码段但在线(这个

5 个答案:

答案 0 :(得分:10)

我个人使用此方法调试Windows服务:

static void Main() {

    if (!Environment.UserInteractive) {
        // We are not in debug mode, startup as service

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new MyServer() };
        ServiceBase.Run(ServicesToRun);
    } else {
        // We are in debug mode, startup as application

        MyServer service = new MyServer();
        service.StartService();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}

MyServer课程中创建一个将使用OnStart事件的新方法:

public void StartService() {
    this.OnStart(new string[0]);
}

答案 1 :(得分:2)

试试这个:

#if DEBUG
while (!System.Diagnostics.Debugger.IsAttached)
{
    Thread.Sleep(1000);
}
System.Diagnostics.Debugger.Break();
#endif

等到你附加一个调试器,然后中断。

答案 2 :(得分:1)

This可能就是你想做的事情

答案 3 :(得分:1)

在CodeBlex中检查此项目
Service Debugger Helper

enter image description here

我亲自使用这个助手。

答案 4 :(得分:0)

我会这样做:
在您的服务的OnStart方法中,将呼叫添加到顶部的Debugger.Break()

protected override void OnStart(string[] args)
{
    #if DEBUG
        Debugger.Break();
    #endif

    // ... the actual code
}
相关问题