如果服务那样启动和停止,则意味着您的代码将引发未处理的异常。这很难调试,但是有一些选择。
If the service starts and stops like that, it means your code is throwing an unhandled exception. This is pretty difficult to debug, but there are a few options.
- 咨询Windows Event Viewer 。通常,您可以转到计算机/服务器管理器,然后单击事件查看器-> Windows日志-> 应用程序来完成此操作。您可以在此处查看引发异常的原因,这可能有所帮助,但您没有获得堆栈跟踪。
- 将程序逻辑提取到库类项目中。现在,创建程序的两个不同版本:控制台应用程序(用于调试)和Windows服务。 (这是一些初期工作,但是从长远来看可以节省很多焦虑。)
- 添加更多try / catch块并登录到应用程序以更好地了解内容
- Consult the Windows Event Viewer. Normally you can get to this by going to the computer/server manager, then clicking Event Viewer -> Windows Logs -> Application. You can see what threw the exception here, which may help, but you don't get the stack trace.
- Extract your program logic into a library class project. Now create two different versions of the program: a console app (for debugging), and the windows service. (This is a bit of initial effort, but saves a lot of angst in the long run.)
- Add more try/catch blocks and logging to the app to get a better picture of what's going on.