AppDomain.AssemblyLoad事件捕获事件处理程序中引发的所有异常 [英] AppDomain.AssemblyLoad event catches all exceptions raised within the event handlers

查看:72
本文介绍了AppDomain.AssemblyLoad事件捕获事件处理程序中引发的所有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET AppDomain.AssemblyLoad 事件似乎捕获了事件处理程序中引发的所有异常,而不是将其传播给触发程序集加载的调用方(例如, Assembly.LoadFile()).

It seems the .NET AppDomain.AssemblyLoad event catches any exceptions thrown within it's event handlers, not propagating them to the caller who triggered the assembly load (e.g. Assembly.LoadFile()).

我的第一个问题是为什么这会捕获所有存在的异常行为?微软通常非常严格地确保异常总是在其BCL中传播.

My first question is why does this catch all exceptions behavior exist? Microsoft are generally pretty strict about ensuring exceptions always propagate in their BCLs.

我的第二个问题,有什么办法可以关闭这种行为?

My second question, is there any way to turn off this behavior?

背景:我需要在程序集加载时对其进行扫描,并确保它们符合某些约定.如果没有,则应该引发异常(并被可能触发程序集加载的多种机制所捕获).

Background: I need to scan assemblies as they are loaded and ensure they comply with some convention. If they don't, an exception should be thrown (and caught higher up by multiple mechanisms that could trigger the assembly load).

注意:我已经找到了这个问题从AppDomain.AssemblyLoad事件抛出异常" ,这不是重复项.从逻辑上讲,我的问题来自该问题的结论.

Note: I already found this question 'Throw exception from AppDomain.AssemblyLoad event', which is not a duplicate. My question logically follows from the conclusion of that question.

推荐答案

否,CLR内吞没了异常,无法更改该行为.大多数AppDomain事件都是这种情况:AssemblyLoad,DomainUnload和ProcessExit.SSCLI20源代码没有注释来指示为什么这样做.

No, exceptions are swallowed inside the CLR, no way to alter that behavior. This is the case for most of the AppDomain events: AssemblyLoad, DomainUnload and ProcessExit. The SSCLI20 source code has no comments that indicate why this is done.

一个粗略的猜测是,代码太深地埋在CLR堆栈框架内,无法进行合理的诊断和展开框架,并且恢复状态太困难了.AssemblyLoad,因为该事件几乎总是在抖动忙于生成代码时引发,而DomainUnload和ProcessExit则由于CLR正在忙于关闭所有代码执行而引发.

A rough guess is that the code is buried too deep inside CLR stack frames to allow a reasonable diagnostic and unwinding frames and restoring state is too difficult. AssemblyLoad because that event is almost always raised while the jitter is busy generating code, DomainUnload and ProcessExit because the CLR is busy shutting down all code execution.

解决方法很明确,如果必须捕获异常,则在事件处理程序中需要try/catch语句.

The workaround is clear, if you must catch exceptions then you need a try/catch statement in your event handler.

这篇关于AppDomain.AssemblyLoad事件捕获事件处理程序中引发的所有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆