Weird behavior when mixing loading of assemblies using Assembly.LoadFrom
and Assembly.Load
:
I have encountered a weird behavior when loading assemblies with Assembly.LoadFrom
and later on with Assembly.Load
.
I am loading an assembly using Assembly.LoadFrom
, where the assembly is located in a folder, which is not the execution folder.
Later on in my test code when I try to load once again this assembly with Assembly.Load
, the load fails with a System.IO.FileNotFoundException
(“Could not load file or assembly…”) despite the fact that the assembly is already loaded. The load fails both with the strong name and the non-strong name (the original reason for loading once again this assembly is a usage of a BinaryFormatter
).
However, in case the assembly is located in the execution folder the later load succeeds in both cases, with the strong name and the non-strong name. In this case, you can see that two identical assemblies are loaded from two different locations.
A simple code sample that recreates this problem:
Assembly assembly1 = Assembly.LoadFrom(@"C:\a.dll");
// Loading with a strong-name fails
Assembly assembly2 = Assembly.Load(@"a, Version=1.0.0.0, Culture=neutral, PublicKeyToken=14986c3f172d1c2c");
// Also loading with a non-strong fails
Assembly assembly3 = Assembly.Load(@"a");
- Any explanation why the CLR ignores the already loaded assembly?
- Any idea how can I alleviate this problem?