Assembly.LoadFile() throws exception about missing manifest

4

The exception is:

The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)

I get it at the line:

Assembly lvAssembly = Assembly.LoadFile(aPathFileName); 

The file being loaded is a plugin that is generated by a different application. If I changed the Target Framework Version of the plugin from 4.0 to 3.5 and recompile. The Plugin loads fine. I don't understand why the Assembly.LoadFile method would care what version of the .net framework the plugin was compiled for.

Changing the Target Framework for the application loading the plugin to 4.0 didn't help.

c#
asked on Stack Overflow Jan 12, 2011 by scott • edited Jan 12, 2011 by Bobby

1 Answer

6

The first part of your question is the expected outcome, CLR version 2 cannot load .NET 4.0 assemblies, the metadata format has changed.

The last paragraph is harder to explain. It might have something to do with using LoadFile(), it's a frikin gawdawful way to load assemblies. One possible failure mode is that your plug-in assembly might still have references to 2.0 assemblies. This is pretty normal when it has a reference to yet another assembly that was compiled to target an earlier framework. This is normally silently resolved by the assembly loader, it just replaces the 2.0 reference with the corresponding 4.0 reference.

You can verify this theory with ildasm.exe, run it on the plug-in assembly and looks through the manifest for .assembly directives. The version number is easy to see, you'd get something like

.assembly extern mscorlib as mscorlib_2
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) 
  .ver 2:0:0:0
}

Always favor Assembly.LoadFrom().

answered on Stack Overflow Jan 12, 2011 by Hans Passant

User contributions licensed under CC BY-SA 3.0