【问题标题】:AssemblyLoad event is not fired in AppDomain在 AppDomain 中未触发 AssemblyLoad 事件
【发布时间】:2013-02-15 10:42:50
【问题描述】:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace DefaultAppDomainApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with the default app domain *****\n");
            InitDAD();

            DisplayDADStats();
            Console.WriteLine();

            ListAllAssembliesInAppDomain();

            Console.ReadLine();
        }

        #region Init the default app domain
        private static void InitDAD()
        {
            // This logic will print out the name of any assembly
            // loaded into the applicaion domain, after it has been
            // created. 
            AppDomain defaultAD = AppDomain.CurrentDomain;
            defaultAD.AssemblyLoad += (o, s) =>
                {
                    Console.WriteLine("{0} has been loaded!", s.LoadedAssembly.GetName().Name);
                };
        }
        #endregion

        #region Display basic stats
        private static void DisplayDADStats()
        {
            // Get access to the app domain for the current thread.
            AppDomain defaultAD = AppDomain.CurrentDomain;

            Console.WriteLine("Name of this domain: {0}", defaultAD.FriendlyName);
            Console.WriteLine("ID of domain in this process: {0}", defaultAD.Id);
            Console.WriteLine("Is this the default domain?: {0}", defaultAD.IsDefaultAppDomain());
            Console.WriteLine("Base directory of this domain: {0}", defaultAD.BaseDirectory);
        } 
        #endregion

        #region List loaded assemblies 
        static void ListAllAssembliesInAppDomain()
        {
            // Get access to the app domain for the current thread.
            AppDomain defaultAD = AppDomain.CurrentDomain;

            // Now get all loaded assemblies in the default app domain. 
            var loadedAssemblies = from a in defaultAD.GetAssemblies() orderby a.GetName().Name select a;

            Console.WriteLine("***** Here are the assemblies loaded in {0} *****\n",
              defaultAD.FriendlyName);
            foreach (var a in loadedAssemblies)
            {
                Console.WriteLine("-> Name: {0}", a.GetName().Name);
                Console.WriteLine("-> Version: {0}\n", a.GetName().Version);
            }
        } 
        #endregion

    }
}

上面的代码来自“Andrew Troelsen”的“Pro C# 2010 and the .NET 4 Platform”一书。 在这里,当我运行此代码时,控件永远不会到达该行

            defaultAD.AssemblyLoad += (o, s) =>
                {
                    Console.WriteLine("{0} has been loaded!", s.LoadedAssembly.GetName().Name);
                };

为什么当我运行这段代码时这个事件没有被触发?什么时候控制到这里?

【问题讨论】:

    标签: c# .net visual-studio-2010


    【解决方案1】:

    由于多种原因,无法访问此事件处理程序。 AppDomain.CurrentDomain 已加载开始执行 Main 方法所需的所有程序集。因此,您添加事件处理程序为时已晚。您需要添加一个特殊的静态方法,.NET 框架会查找并将执行以运行您的应用程序初始化代码,它称为 AppInitialize 并且您将在那里绑定您的处理程序。对 AppInitialize 进行一些挖掘。

    您的域也不是唯一涉及的域。肯定至少有一个其他共享应用程序域,所有 GAC 和完全受信任的程序集都被加载到其中。此外,根据应用程序的配置方式以及其他程序集如何加载其依赖项,可能还有其他应用程序域。在 MSDN 上对 Application Domains 主题进行尽职调查。

    【讨论】:

      【解决方案2】:

      根据文档(以及我的经验),该事件仅在使用 Assembly.Load 方法之一时触发。

      当运行时自动解析并加载程序集时,它不会触发。

      【讨论】:

      • docs 是在哪里说的?至少当前版本只是说“加载程序集时发生。”
      • 这不是正确的答案。正确答案在下面找到 - 您添加事件处理程序为时已晚,无法捕获任何内容。
      【解决方案3】:

      事件不会触发,因为当应用程序启动时,所有引用的程序集都已加载(以防您没有动态加载程序集)。

      【讨论】:

        【解决方案4】:

        我也遇到了关于 AssemblyLoad 的问题。我不得不使用以下设置对其进行调查:

        1. 在 ASP.Net 应用程序启动期间,在配置 DI 时,向 AssemblyLoad 事件添加了一个事件处理程序,记录所有加载的程序集和 AppDomain FirendlyName。
        2. 之后,所有已加载的程序集也会被记录。
        3. 在其他地方的控制器构造函数中,所有加载的程序集都被再次记录。
        4. 所有依赖都在 GAC 中。

        结果:
        所有日志都显示相同的 AppDomain 名称,但控制器日志显示的加载程序集比最初由 (2) 和 AssemblyLoad 事件处理程序 (1) 记录的组合更多。所以看起来甚至没有为所有加载的程序集触发 AssemblyLoad。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-15
          • 2017-07-30
          • 2014-03-13
          • 2014-03-07
          相关资源
          最近更新 更多