.netexceptionassembly-resolutionassemblyresolve

.NET 4 loads assemblies different from .NET 3.5


With migrating to .net 4 we started facing the problem with our library. Assume we have our library MyLib.dll and it references interop assembly Interop.dll. Interop.dll has reference to MissingInterop.dll.

So the references can be shown as: MyLib.dll -> Interop.dll -> MissingInterop.dll

In MyLib.dll we use only part of classes from Interop.dll so we never call anything that needs MissingInterop.dll and in .net 3.5 it works just fine That's why we don't ship MissingInterop.dll with MyLib.dll.

When we use MyLib.dll from process running under .net 4 application fails with the following exception:

FileNotFoundException: "Could not load file or assembly 'MissingInterop.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."`

Also i have noticed that Interop.dll references other missing dll's but .net doesn't try to load them. And i have found the difference. Some types from MissingInterop.dll are used in method parameters in Interop.dll while types from other missing libraries are used only as method return types, i.e. in Interop.dll i can see:

  Class C1
     MethodA : void (valuetype [MissingInterop]MissingAssembly.TypeA&)
  Class C2
     get_Status : valuetype[AnotherMissingInterop]AnotherMissingAssembly.TypeB()

and .NET 4 tries to load MissingInterop.dll but doesn't try to load AnotherMissingInterop.dll.

.NET 3.5 didn't try to load neither MissingInterop.dll nor AnotherMissingInterop because they are not used in execution path.

I know that .NET 4 has new Fusion but i haven't found such breaking change described anywhere. Does anybody know why .NET 4 tries to load something that is not needed? Is there a way to fix this without recompiling the code or adding missing file?


Solution

  • When you are migrating your application from .NET 3.0, 3.5 to 4.0, then you need to include this mylib.dll into your project externally and compile them or if possible you have the source code of assembly then change it into .net 4.0