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

查看:32
本文介绍了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()).

我的第一个问题是为什么这会捕获所有异常行为?Microsoft 通常非常严格地确保异常始终在其 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 因为该事件几乎总是在 jitter 忙于生成代码、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天全站免登陆