在追求的同时a spearate problem我遇到了一个非常奇怪的情况。演示代码将是:
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Log("In Application_Start");
SomeClass.SomeProp = ConfigurationManager.AppSettings["PropValue"];
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
Log("In Application_BeginRequest");
try
{
this.Application_Start(null, null);
}
catch ( Exception ex )
{
Log(ex.ToString());
}
Log("At the end of Application_BeginRequest");
}
}
我在日志中得到的是:
In Application_BeginRequest
Could not load file or assembly 'vjslib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
System.IO.FileNotFoundException
at MyRootNamespace.Global.Application_Start(Object sender, EventArgs e)
at MyRootNamespace.Global.Application_BeginRequest(Object sender, EventArgs e) in D:\My Documents\Visual Studio 2008\Projects\SolutionDir\ProjectDir\Global.asax.cs:line 109
At the end of Application_BeginRequest
这对我来说毫无意义。考虑:
vjslib
由我的主要项目(程序集)引用,其中包括 Global
类(class)。如果无法解析其依赖项,为什么要加载该程序集? SomeClass
位于另一个程序集中,该程序集也引用了 vjslib
. SomeClass
确实使用 vjslib
并且某些成员确实公开了从 vjslib
中的类派生的类,但这里使用的属性只是一个普通的旧字符串。 依赖项是否在每个方法的基础上解决?我以为Microsoft doesn't do such things anymore .这里发生了什么?
最佳答案
我相信当 CLR 在 IL 中遇到对某种类型的引用时,它会尝试加载它。它们可能会导致加载程序集。因此,所有依赖程序集不一定在启动时加载 - 它们将按需加载。
编辑:在 SO 上看到这个问题关于程序集何时加载。 “CLR via C#”一书还谈到了在 IL 中的 JIT 遇到输入时程序集被加载。
关于.net - .net 何时检查程序集依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3707835/