简体   繁体   English

从AppDomain.AssemblyLoad事件中抛出异常

[英]Throw exception from AppDomain.AssemblyLoad event

Can someone explain to me why I cannot seem to throw an exception from inside the AppDomain.Assembly load event? 有人可以向我解释为什么我似乎无法从AppDomain.Assembly加载事件中抛出异常? For example: 例如:

class Program
{
    static Program()
    {
        AppDomain.CurrentDomain.UnhandledException += (s, a) =>
        {
            Console.WriteLine("Caught exception!");
        };

        AppDomain.CurrentDomain.AssemblyLoad += (s, a) =>
        {
            Console.WriteLine(string.Format("Assembly {0} loaded", a.LoadedAssembly.FullName));

            throw new Exception();

            Console.WriteLine("Should never get here...");
        };
    }

    static void Main(string[] args)
    {
        Console.WriteLine(new ClassLibrary1.Class1().TestString());
        Console.WriteLine();
        Console.WriteLine("Done...");
        Console.ReadLine();
    }
}

When I execute this, the output is as follows: 执行此操作时,输出如下:

Assembly ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null loaded
TestString
Done...

Can anyone explain this behavior to me? 任何人都可以向我解释这种行为吗? Thank you. 谢谢。

EDIT To clarify a couple things: 编辑澄清一些事情:

  • The assembly load event runs fine, when I expect it to run. 当我希望它运行时,程序集加载事件运行正常。 But my exception never gets thrown 但我的异常永远不会被抛出

  • This is a distilled example taken from a larger application. 这是一个从更大的应用程序中提取的蒸馏示例。 I want to inspect the assembly after it is loaded and if I don't like something about it, I want to fail fast... But my exception doesn't 'happen' 我想在装载后检查装配,如果我不喜欢它,我想快速失败......但是我的例外不会“发生”

This happens because of the way the JIT compiler works. 这是因为JIT编译器的工作方式。 It needs to generate the code for the Main() method before it can start to run. 它需要在Main()方法开始运行之前生成代码。 Since you are referencing the ClassLibrary1.Class1() type, it needs to load that assembly to retrieve type info. 由于您引用了ClassLibrary1.Class1()类型,因此需要加载该程序集以检索类型信息。 That requires it to load the assembly before your code starts running. 这需要它代码开始运行之前加载程序集。 Change it like this to get the exception: 像这样更改以获得异常:

using System.Runtime.CompilerServices;
...
    static void Main(string[] args) {
        Test();
    }
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Test() {
        Console.WriteLine(new ClassLibrary1.Class1().TestString());
        Console.WriteLine();
        Console.WriteLine("Done...");
        Console.ReadLine();
    }

Now the static constructor can run first and register the AssemblyLoad event handler before the ClassLibrary1 assembly gets loaded. 现在,静态构造函数可以先运行,并在加载ClassLibrary1程序集之前注册AssemblyLoad事件处理程序。

The exception is being thrown. 抛出异常。 But it seems .Net sometimes ignores exceptions happening in startup (Main()). 但似乎.Net有时会忽略启动时发生的异常(Main())。 I'm not sure of the reason, but I usually go to Debug->Exceptions and check the box "Throw" for Common Language Runtime Exceptions" to be able to break at the exception. 我不确定原因,但我通常会进入Debug-> Exceptions并选中“抛出公共语言运行时异常”框,以便能够在异常处中断。

Why do you think the exception is not being thrown? 为什么你认为异常没有被抛出? If it weren't being thrown, one would expected to see your "Should never get here..." output. 如果它没有被抛出,人们会期望看到你的“永远不会到达......”输出。 However, since it's not there, the exception presumably is being thrown. 但是,由于它不存在,所以可能会抛出异常。

Your code not catching the exception is a whole other story. 您的代码没有捕获异常是另一个故事。 It's quite possible that the code that raises the AppDomain.AssemblyLoad event is catching exceptions. 提升AppDomain.AssemblyLoad事件的代码很可能是捕获异常。

I believe the assembly load event is happening on a separate thread,using asynccallback. 我相信程序集加载事件发生在一个单独的线程上,使用asynccallback。 You are not getting the exception because you need to use Application.ThreadException+=new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); 您没有得到异常,因为您需要使用Application.ThreadException + = new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

I think, I'm not an expert on this at all 我想,我根本就不是这方面的专家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM