如何处理ASP.NET中未处理的线程异常?

栏目: ASP.NET · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/11117167/how-to-treat-unhandled-thread-exceptions-in-asp-net

如何处理ASP.NET应用程序处理非请求后台线程上发生的未处理异常(由于错误)?

默认情况下,此类异常会导致进程终止.这在ASP.NET工作进程的设置中是不可接受的,因为并发运行的请求会无法预料地中止.这也是一个性能问题.

请求线程上的异常不是问题,因为ASP.NET处理它们(通过显示错误页面).

AppDomain.UnhandledException事件允许观察发生了异常,但此时无法阻止终止.

这是一个需要粘贴到ASPX页面代码隐藏的repro.

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();
}

我所知道的唯一解决方案是永远不要让异常“逃避”未经处理.对此还有其他更全面彻底的解决方案吗?

Rx(反应式编程)诞生于解决此类问题,尝试考虑更改当前使用的框架并将其替换为Rx

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

Nugget套餐:

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

这是等效的Rx代码:

var o = Observable.Start(() => { throw new NotImplementedException(); });

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

正如您所看到的,当您为错误指定处理程序时,错误不会转义后台线程,onError => {}

如果未指定错误处理程序,则会传播异常:

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

在上面的示例中,异常将被传播,并将导致与发布的代码相同的问题

翻译自:https://stackoverflow.com/questions/11117167/how-to-treat-unhandled-thread-exceptions-in-asp-net


以上所述就是小编给大家介绍的《如何处理ASP.NET中未处理的线程异常?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to the Design and Analysis of Algorithms

Introduction to the Design and Analysis of Algorithms

Anany Levitin / Addison Wesley / 2011-10-10 / USD 117.00

Based on a new classification of algorithm design techniques and a clear delineation of analysis methods, Introduction to the Design and Analysis of Algorithms presents the subject in a coherent a......一起来看看 《Introduction to the Design and Analysis of Algorithms》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具