我想调试窗口服务。我应该在 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/