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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With