【发布时间】:2013-04-03 18:03:48
【问题描述】:
我需要我的服务在启动过程中检查某些文件的存在和结构,如果不满足某些条件则退出/失败/停止。 我读了这个帖子:What is the proper way for a Windows service to fail? 但这无济于事。
我将 ServiceBase.ExitCode 属性设置为非零,然后调用 ServiceBase.Stop。但我得到 5 个事件日志条目。见下文:
Starting service. (I log this event via code)
Config.xml file not found. (I log this ERROR event via code)
Service stopped successfully. (SCM logs this message)
Service started successfully. (SCM logs this message)
Service cannot be started. The handle is invalid (SCM logs this message)
如您所见,除了最后两个条目外,一切正常。他们为什么在那里?如何在启动期间正确关闭服务?为什么 SCM 看不到服务已停止/失败?
【问题讨论】:
-
我发现抛出异常而不是调用 Stop() 会阻止调用 OnStop 事件,并且我怀疑不会记录您看到的“成功停止”消息。到目前为止,我唯一能找到的似乎是可能的退出代码列表的是hiteksoftware.com/mize/Knowledge/articles/049.htm。如果我使用不在列表中的退出代码,我会收到类似"Error 10501: 0x2905" 的消息(x2905 = 十六进制值 10501)。是否没有关于退出代码的 MSDN 文档,或关于如何向用户发出如何调试问题的信号的指导?
标签: c# service windows-services