How to handle raw threading exceptions in ASP.NET?

How should an ASP.NET application deal with unhandled exceptions that occur in the background thread without a request (due to errors)?

By default, such exceptions will terminate the process. This is not acceptable in setting up an ASP.NET workflow because concurrent execution of requests is interrupted unpredictably. This is also a performance issue.

Exceptions in the request stream are not a problem because ASP.NET processes them (showing the error page).

The AppDomain.UnhandledException event indicates that an exception has occurred, but termination cannot be prevented at this point.

Here is the registry that you need to insert into the ASPX code page.

 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 that I know of is to never let the exception “run” unhandled. Is there any other, more global and thorough solution to this?

+8
multithreading exception unhandled-exception
source share
3 answers

Rx (Reactive Programming) was born to solve such problems, try to consider the possibility of changing your environment and replace it with Rx

http://msdn.microsoft.com/en-us/data/gg577609.aspx

Nugget Packages:

https://nuget.org/packages/Rx-Main/1.0.11226

This is the equivalent Rx code:

  var o = Observable.Start(() => { throw new NotImplementedException(); }); o.Subscribe( onNext => { }, onError => { }, () => { Console.WriteLine("Operation done"); }); 

As you can see, the error will not exit the background stream when specifying the error handler, onError => { }

If you do not specify an error handler, an exception will be thrown:

  o.Subscribe( onNext => { }, () => { Console.WriteLine("Operation done"); }); 

In the above example, an exception will be thrown and cause the same problems as your published code

0
source share

You can mark an exception that is handled in the unhandled exception handler. https://msdn.microsoft.com/en-us/library/system.windows.applicationunhandledexceptioneventargs.handled(v=vs.95)

This should prevent the workflow from returning due to an error in the background thread.

However, you would not do this for all exceptions. Only those you know are safe to ignore.

0
source share

I think "legacyUnhandledExceptionPolicy" is your answer.

Unhandled exceptions that cause ASP.NET to crash in .NET 2.0; http://blogs.msdn.com/b/tom/archive/2007/12/04/unhandled-exceptions-causing-asp-net-to-crash-in-net-2-0.aspx

0
source share

All Articles