c# - 调试窗口服务

标签 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

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

最佳答案

我个人用这个方法来调试一个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]);
}

关于c# - 调试窗口服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6995565/

相关文章:

C# 控制台 : how can i read piped input from the command line

objective-c - OSX Lion 和 Movist : playback freezes at specific instant

python - 用于控制 Python 调试消息日志记录的首选方法?

c++ - 如何从Visual C++中获取具体的windows服务可执行文件路径

c# - 从窗口服务显示窗口窗体

c# - 为什么我的 CSS 没有被缩小?

C# 泛型 - 返回派生类的对象?

node.js - 有没有办法让 chrome-devtools (--inspect) 自动从已结束的进程中分离?

Windows 服务中的 WCF 安全性

c# - 在没有 controller/http.context 的情况下绑定(bind)模型