2008-10-22

BadImageFormatException when loading Microsoft.TeamFoundation.Client.dll

I tried to make a small winforms application on my 64bit Vista that talks with our TeamFoundationServer today. I use a plugin based application that loads the assemblies using Assembly.Load (utilizing the AppDomain.AssemblyResolve event).

The problem surfaced when I tried to activate a function in one of my loaded plugins that used the TeamFoundation client dll, this was not located in the same folder as the exe.
Using the AppDomain.CurrentDomain.AssemblyResolve event, I was easy to find the assembly dll and using Assembly.LoadFrom(filename) I could return the needed assembly.

The problem was that when I tried to do this, I got a BadImageFormatException stating that

Could not load file or assembly 'file:///C:\MyPath\MyProject\bin\Debug\Plugins\MyPlugin\Microsoft.TeamFoundation.Client.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Meridium.Plugins.PluginLoader.CurrentDomain_AssemblyResolve(Object sender, ResolveEventArgs args) in C:\VSS\TFS\Meridium\Source\Meridium.Plugins\PluginLoader.cs:line 606

This seemed not quite correct, since the assembly at the location is ok and can be opened for instance in Reflector.

After a bit of searching I found more clues pointing to the fact that a 64bit application cannot load 32bit assemblies if they are in "Mixed mode" since they contain native code and thus are platform specific. The forum post http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=665884&SiteID=1 stated the problem and provided the solution to set the project output to be built for the x86 platform.

After changing the settings in the project, it worked like a charm.

No comments:

Post a Comment