Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# AppDomain中未激发AssemblyLoad事件_C#_.net_Visual Studio 2010 - Fatal编程技术网

C# AppDomain中未激发AssemblyLoad事件

C# AppDomain中未激发AssemblyLoad事件,c#,.net,visual-studio-2010,C#,.net,Visual Studio 2010,以上代码是我从Andrew Troelsen的《Pro C#2010和.NET 4平台》一书中获得的。 在这里,当我运行此代码时,控件从未到达该行 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace DefaultAppDomainApp { class Program {

以上代码是我从Andrew Troelsen的《Pro C#2010和.NET 4平台》一书中获得的。 在这里,当我运行此代码时,控件从未到达该行

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

    }
}

为什么在运行此代码时未触发此事件?当控件到达此处时?

事件不会激发,因为当应用程序启动时,所有引用的程序集都已加载(在您没有动态加载程序集的情况下)。

事件不会激发,因为当应用程序启动时,所有引用的程序集都已加载(在没有动态加载程序集的情况下)。

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

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

根据文档(以及我所经历的),事件仅在使用了
assembly.Load
方法之一时才会触发


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

由于以下几个原因,无法访问此事件处理程序。AppDomain.CurrentDomain已加载开始执行主方法所需的所有程序集。因此,您添加事件处理程序的时间太晚了。您需要添加一个特殊的静态方法ramework查找并将执行应用程序初始化代码,它被称为AppInitialize,您可以将处理程序绑定到那里。在AppInitialize上进行一些挖掘


此外,您的域不是唯一涉及的域。肯定至少有一个其他共享应用程序域可以将所有GAC和完全受信任的程序集加载到其中。此外,根据应用程序的配置方式和其他程序集加载其依赖项的方式,可能还有其他应用程序域。请对MSD进行尽职调查N关于应用程序域主题。

由于以下几个原因,无法访问此事件处理程序。AppDomain.CurrentDomain已加载开始执行主方法所需的所有程序集。因此,添加事件处理程序太晚了。您需要添加一个特殊的静态方法,.NET framework将查找并执行该方法以运行我们的appinit代码称为AppInitialize,您可以将您的处理程序绑定到那里。对AppInitialize进行一些挖掘


此外,您的域不是唯一涉及的域。肯定至少有一个其他共享应用程序域可以将所有GAC和完全受信任的程序集加载到其中。此外,根据应用程序的配置方式和其他程序集加载其依赖项的方式,可能还有其他应用程序域。请对MSD进行尽职调查N关于应用程序域的主题。

我还面临AssemblyLoad的问题。我必须通过以下设置进行调查:

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

结果:

所有日志显示相同的AppDomain名称,但控制器日志显示的加载程序集多于最初由(2)和AssemblyLoad事件处理程序(1)记录的程序集组合。因此,它看起来甚至没有为所有加载的程序集启动AssemblyLoad。

我也面临AssemblyLoad的问题。我必须通过以下设置进行调查:

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

结果:

所有日志都显示相同的AppDomain名称,但控制器日志显示的加载程序集比最初由(2)和AssemblyLoad事件处理程序(1)记录的程序集总和还要多。因此,它看起来不像是为所有加载的程序集启动了AssemblyLoad。

在哪里这样说?至少当前版本只是这样说“加载程序集时发生”。这不是正确答案。下面是正确答案-您添加事件处理程序太晚,无法捕获任何内容。在哪里会这样说?至少当前版本只是说“加载程序集时发生”。“这不是正确答案。下面是正确答案-您添加事件处理程序太晚,无法捕获任何内容。
            defaultAD.AssemblyLoad += (o, s) =>
                {
                    Console.WriteLine("{0} has been loaded!", s.LoadedAssembly.GetName().Name);
                };