问题描述
在追求时,我来了非常奇怪的情况。演示代码将是:
While pursuing a spearate problem I've come to a very peculiar situation. A demo code would be:
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");
}
}
我在日志中的内容是:
What I get in my log is:
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
这对我来说没有任何意义。考虑:
This makes no sense to me whatsoever. Consider:
-
vjslib
由我的主项目(程序集)引用,其中包括全局
类。如果依赖关系无法解决,为什么程序集加载? -
SomeClass
在另一个程序集中也引用vjslib
。SomeClass
使用vjslib
,某些成员会公开从vjslib
,但这里使用的属性只是一个简单的旧字符串。 - 为什么堆栈跟踪的第一行没有行号?
vjslib
is referenced by my main project (assembly) which includes theGlobal
class. Why was the assembly loaded at all if its dependencies could not be resolved?SomeClass
is in another assembly which also referencesvjslib
.SomeClass
does usevjslib
and some members do expose classes that are derived from classes invjslib
, but the property used here is just a plain old string.- Why is there no line number in the first line of the stack trace?
依赖方式是否按每个方法解决?我以为。
Are the dependencies resolved on a per-method basis? I thought that Microsoft doesn't do such things anymore. What's going on here?
推荐答案
我相信当CLR遇到引用IL中的某些类型时,会尝试加载它。并且它们可能导致加载组件。所以所有依赖程序集不一定在启动时加载 - 它们将按需加载。
I believe that when CLR encounter reference to some type in IL, it attempts to load it. And they may result in loading the assembly. So all dependent assemblies does not necessarily get loaded at the start up - they will be getting loaded on demand.
编辑:在关于装配的加载。 CLR通过C#这本书还谈到在IL中JIT遇到的类型时加载的程序集。
See this question on SO about when assembly gets loaded. The book "CLR via C#" also talks about assembly getting loaded when type in encountered by JIT in IL.
这篇关于netnet什么时候检查程序集的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!