安装 WindowsService 用于调试
我正在尝试启动调试构建 WindowsService
但收到错误消息,表明它没有及时启动。
我已经清理了 ctor 和 OnStart ,但它仍然无法启动,所以我想也许它加载的 dll 需要更好地安装,而不是仅仅安装在我的项目目录的调试目录中。也许 main()
中的所有凹凸都将其晾干?
有调试服务的指导吗?我无法直接在 VStudio 中运行它,因为显然这不允许访问其 OnCustomCommand(int cmd)
成员并在那里中断它。
I am trying to start my debug build WindowsService
but am getting the error that it didn't start in a timely fashion.
I've cleaned up the ctor and OnStart
but still it won't start so I am thinking maybe the dll it loads needs to be better installed, rather than just in the debug directory of my project directory. Maybe all that bumpf in main()
is hanging it out to dry?
Any pointers for debugging the service? I can't run it directly in the VStudio because obviously that doesn't permit entry to its OnCustomCommand(int cmd)
member and breaks it there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您希望能够在服务处于空闲“已启动”状态之前对其进行调试吗?
您可以使用 Debugger.Break() 函数为了这。例如,我们经常在调试版本中将以下内容放入 Main 函数中:
If I understand correctly, you want to be able to debug the service before it's sitting in it's idle "Started" state?
You can use Debugger.Break() function for this. For example, we often put the following into our Main function while in a debug build:
我只会在 OnStart 方法中做尽可能少的事情,足够的 init 来启动一个完成所有工作的线程,这样您就可以尽快从 OnStart 返回。然后在线程委托中,您可以添加 Reddog 的代码来闯入调试器,可能会在其中添加 thread.sleep ,以便您有时间首先附加到进程。
另一种选择是向您的服务添加一个 main 方法,该方法可以创建服务类的实例并调用 onstart/onstop,并将项目输出更改为控制台应用程序,然后您将获得两全其美的效果,它将安装为通过 installutil 提供的服务,您可以 f5 运行它并在 Visual Studio 中将其作为控制台应用程序进行调试。
详细信息此处
如果您想仅将服务作为服务进行调试,那么我建议您的 OnStart 方法启动一个执行处理然后返回的线程,然后线程委托可以执行 Thread.Sleep(一些合理的时间),然后调试器.Break();
线程睡眠使您有机会将调试器附加到进程,在使用服务控制管理器启动服务后,它会到达 Debugger.Break(),这将强制调试器中断,从而允许您单步执行。关键是在 30 秒之前从 OnStart 返回,并在到达断点之前将调试器附加到进程。
另外,我认为 Debugger.Break 为您提供了附加调试器的选项,因此请在代码中尝试(不确定它是否与服务很好地配合,因为它们应该没有 UI),您可能会摆脱坚持 Debugger.Break进入您的代表并在 30 秒之前附加到流程。
I would only do as little as possible in the OnStart method, enough init to kick off a thread that does all the work, so you can return from OnStart as quickly as you can. Then in the thread delegate you can add Reddog's code to break into the debugger, possibly with a thread.sleep in there so that it gives you time to attach to the process first.
Another alternative is to add a main method to your service, which can create an instance of the service class and call onstart/onstop, and change the project output to console app, then you get the best of both worlds, it'll install as a service via installutil, and you can f5 run it and debug it in visual studio as a console app.
Details here
If you want to debug the service only as a service, then I would suggest that your OnStart method starts a thread that does the processing and then returns, the thread delegate then can do a Thread.Sleep(some reasonable amount of time) and then Debugger.Break();
The thread sleep gives you a chance to attach the debugger to the process, after you have started the service using service control manager, then it gets to the Debugger.Break() which will force the debugger to break, allowing you to single step. The key thing is to return from OnStart before 30 seconds, and to attach the debugger to the process before you hit your breakpoints.
Also, I think Debugger.Break gives you the option to attach the debugger, so try that in the code (not sure if it plays nicely with services though as they are supposed to have no UI) you might get away with sticking Debugger.Break into your delegate and attaching to the process before 30 secs.