我们如何跟踪 Windows 服务启动时的程序集加载顺序?
例如。当我们启动服务时,它会加载所有引用程序集及其依赖项;我想做的是在实际启动服务之前加载哪些程序集(OS、CLR 等)。
最佳答案
为此,您可以在 AppDomain.CurrentDomain
上使用 AssemblyLoad
事件。
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
Assembly.Load("ICSharpCode.SharpZipLib");
Console.WriteLine("Completed loading");
/*
* This produced:
Loaded assembly C:\Documents and Settings\...\ConsoleApplication2\bin\Debug\ICSharpCode.SharpZipLib.dll
Completed loading
*/
}
static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
{
Console.WriteLine("Loaded assembly " + args.LoadedAssembly.Location);
}
请注意,这仅适用于从您添加事件的位置加载的程序集。例如,mscorlib
已在调用 Main
之前加载,因为您需要它才能运行 Main
。
当然,如果您自己创建域,您可以在域启动之前添加此事件,您应该会看到所有已加载的程序集。
关于.net - 服务启动时的 DLL 加载顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4152105/