以下是Windows服务程序的常规program.cs内容:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
}
}
把
ServiceBase.Run(...)
在一个试抓区?
谢谢。
编辑:
执行了一些测试并找到(测试方法:向服务发送自定义命令,该命令在OnCustomCommand重写中引发ApplicationException):
a.Try/Catch中的封闭ServiceBase.Run()未捕获OnCustomCommand中引发的异常,因为服务线程开始执行时,Try块已在作用域中。因此,质疑这种方法的正确性是无关紧要的,只要它不满足它的目的。
b.为appdomain.currentdomain.unhandledException添加处理程序也未捕获该异常。
但是,在这两种情况下,异常都显示在Windows事件日志中。这基本上解决了我在服务执行过程中知道什么时候崩溃的需求,但问题仍然存在:在事件日志中没有任何跟踪的情况下,是否有任何情况下服务可能会悄悄崩溃?