.NET 4 loads assemblies different from .NET 3.5

1k views Asked by At

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?

2

There are 2 answers

1
Udit Gupta On BEST ANSWER

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

0
Ian P On

Probably not (from experience). There were similar issues around an earlier step (cant remember if it was 2.0 -> 2.1 or 3.0 -> 3.1. but it was a while ago) We had machines reporting that the earlier version was installed, and crashing. When we cleaned out the net installs and actually installed the lower version, and then the update, all worked fine. Microsoft may sneak out a patch. I'd be inclined to actually raise it with MS. You have a seemingly well reasearched bug, and in such circumsatances support contract or not, they are very reluctant to end up charging you. (My experience anyway)