StackOverflowException in a .Net Windows Service

And*_*ish 1 .net stack-overflow windows-services

In some circumstances my .Net windows service can generate a StackOverflowException. Unfortunately, the behaviour seems to be that the service simply stops dead and doesn't write anything into the event log. I don't even get a message from the service control manager saying the service has failed.

is there any way at all a windows service can detect that such an exception has occurred?

In the documentation for this exception, MSDN says "Note that an application that hosts the common language runtime (CLR) can specify that the CLR unload the application domain where the stack overflow exception occurs and let the corresponding process continue". this is the kind of thing I would expect the windows service implementation to do, but it doesn't.

请不要回复说我应该确保我的代码永远不会抛出这样的例外 - 相信我,我会尽我所能 - 我想要做的是以合理的方式处理最坏的情况并使我的服务具有弹性意外错误.

Han*_*ant 5

堆栈溢出是线程可能遭受的最严重的心脏病发作.这是非常糟糕的,你甚至没有在事件日志中得到一些东西.这是非常糟糕的,你甚至无法做任何合理的事情来恢复你的程序状态.线程已经死了,appdomain的状态也是如此.它以完全不可预测的方式发生变异,你只能扔掉它.

好吧,你已经知道了这一切.但是耸耸肩并假装它没有发生会导致另一种失败.系统故障,该服务应该做一些事情,但没有发生.没有很多场景可以接受.没有处理文件,没有发生数据库更新,等等.这种可能导致一连串意外事故发生的事故.就像首席财务官发现在年底失去了一百万美元.

你不想听到这个,但没有明智的方法来处理这个问题.将所有努力集中在发现错误上,而不是创可贴.堆栈溢出始终是一个编程错误.