When does .net check for assembly dependencies?

穿精又带淫゛_ 提交于 2019-12-22 08:35:42

问题


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 is referenced by my main project (assembly) which includes the Global class. Why was the assembly loaded at all if its dependencies could not be resolved?
  • SomeClass is in another assembly which also references vjslib. SomeClass does use vjslib and some members do expose classes that are derived from classes in vjslib, 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?


回答1:


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.

Edit: 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.



来源:https://stackoverflow.com/questions/3707835/when-does-net-check-for-assembly-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!