调试窗口服务
我想调试窗口服务。 我应该在main()中写什么来启用窗口服务中的调试。 我正在使用C#开发窗口服务。
#if(DEBUG) System.Diagnostics.Debugger.Break(); this.OnStart(null); System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); #else ServiceBase.Run(this); #endif
我写了上面的代码段,但在线(这
我会这样做:
在您的服务的OnStart
方法中,在顶部添加对Debugger.Break()
的调用:
protected override void OnStart(string[] args) { #if DEBUG Debugger.Break(); #endif // ... the actual code }
我个人使用这种方法来调试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]); }
试试这个:
#if DEBUG while (!System.Diagnostics.Debugger.IsAttached) { Thread.Sleep(1000); } System.Diagnostics.Debugger.Break(); #endif
它等待你连接调试器, 然后中断。
这可能是你想要做的
在CodeBlex中检查此项目
服务调试器助手
我亲自使用这个助手。