How is an ASP.NET application supposed to deal with unhandled exceptions which are occuring on a non-request background thread (due to bugs)?
By default, such exceptions cause the process to terminate. This is unacceptable in the setting of an ASP.NET worker process because concurrently running requests are aborted unpredictably. It is also a performance problem.
Exceptions on a request thread are not a problem because ASP.NET handles them (by showing an error page).
The AppDomain.UnhandledException
event allows to observe that an exception has occurred, but termination cannot be prevented at that point.
Here is a repro which needs to be pasted into an ASPX page codebehind.
protected void Page_Load(object sender, EventArgs e)
{
var thread = new Thread(() =>
{
throw new InvalidOperationException("some failure on a helper thread");
});
thread.Start();
thread.Join();
}
The only solution I know of is to never let an exception "escape" unhandled. Is there any other, more global and thorough solution for this?