Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
597 views
in Technique[技术] by (71.8m points)

c# - Not all assemblies are being loaded into AppDomain from the bin folder

I have the following method that should retrieve a list of loaded local (in bin folder) assemblies:

static IEnumerable<Assembly> GetLocalAssemblies()
    {
        Assembly callingAssembly = Assembly.GetCallingAssembly();
        string path = new Uri(Path.GetDirectoryName(callingAssembly.CodeBase)).AbsolutePath;

        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
        return assemblies.Where(x => !x.IsDynamic && new Uri(x.CodeBase).AbsolutePath.Contains(path)).ToList();
    }  

But, the list of assemblies is missing a couple assemblies that I need it to have. The assemblies I need are managed (c# .net 4), are referenced in the project, and are present in the bin folder.

Why are binaries that are present in the bin folder NOT swept into the AppDomain when the application starts?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Adil has it, but in more detail:

The .NET CLR uses Just-In-Time compilation. Among other things, this means it loads assemblies on first use. So, despite assemblies being referenced by an assembly in use, if the references haven't yet been needed by the CLR to execute the program, they're not loaded and so will not appear in the list of assemblies in the current AppDomain.

Another thing which may or may not apply, is that if you have the same version of the assembly in the GAC, the CLR uses the GAC preferentially over local assemblies, UNLESS the path to those assemblies is specified in the DEVPATH environment variable. If this is the case and the CLR is using the GAC copy of any of the "missing" assemblies, they'll have differing CodeBase values and won't show up in your Linq query results.

One other thing: you may want to consider using the Location property instead of the CodeBase property. The Location property contains the absolute path to the assembly that was loaded at runtime. The CodeBase property is slightly different, and may not be the same for all assemblies in a full build of a project.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...