.NET 4 loads assemblies different from .NET 3.5
Asked Answered
P

2

16

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?

Proconsul answered 27/1, 2012 at 14:31 Comment(4)
Yes, big changes in .NET 4 to enable the "embed interop types" feature. You broke the warranty before, it worked by accident. Use [ComImport] to provide the missing types.Thulium
Maybe it's because one of the reference is build against framework 3.5. You should try to add useLegacyV2RuntimeActivationPolicy="true" as described here: #1605163Hards
Benoit, thanks for suggestion but it didn't help.Proconsul
As far as I know, clr 4 has many differences from previous releases. If you can reproduce this constantly, then it is kind of by design. As the fix is easy and obvious, I don't think Microsoft should fix it.Clermontferrand
R
0

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

Russian answered 22/2, 2012 at 10:28 Comment(1)
We did that. But the question was why clr 2.0 is ok with this situation and clr 4.0 is not.Proconsul
T
1

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)

Tonry answered 14/2, 2012 at 11:49 Comment(0)
R
0

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

Russian answered 22/2, 2012 at 10:28 Comment(1)
We did that. But the question was why clr 2.0 is ok with this situation and clr 4.0 is not.Proconsul

© 2022 - 2024 — McMap. All rights reserved.