SGEN: An attempt was made to load an assembly with an incorrect format (TFS 2010)

While I was setting up a new build server, I ran into a problem where I would receive the error “SGEN: An attempt was made to load an assembly with an incorrect format” when it would attempt to build my solution.  It turns out it has to do with the fact that my solution contains mixed platform projects, even though I had my Configuration (in the Build Definition) set to Mixed Platforms|Release.

Several suggestions were made on the MSDN forums and Stack Overflow that didn’t make the error go away.  I finally found a solution thanks to a user named wesen on the MSDN forums: see the post here.

Basically the build process is running the 64bit version of sgen.exe, which works great for X64/AnyCpu projects, but if you have a project set for X86 you’re out of luck.  To force it to call the X86 sgen.exe (which still seems to be ok for the AnyCpu assemblies) you will need to set the Tools Version to X86.  You can do that by following these steps:

  1. In Team Explorer, right click on your Build Definition and choose Open Process File Location
  2. Double click on the XAML file that is selected
  3. In the designer, select the container called Sequence (this is the top-level container that goes around everything else).
  4. In the Arguments list (typically at the bottom), change MSBuildPlatform from Microsoft.TeamFoundation.Build.Workflow.Activities.ToolPlatform.Auto to Microsoft.TeamFoundation.Build.Workflow.Activities.ToolPlatform.X86.
  5. Save and close the file.
  6. Check the file back in to TFS and try your build again.

This solution did the trick!  My builds are now working exactly as I expect them to.

Another suggestion I saw (which didn’t help me at all), was to disable the project from creating a serialization assembly.  Follow these steps to do that (it may or may not help, but it’s worth a try):

  1. In Solution Explorer, right click on the project that has the error and go to Properties.
  2. In the Build tab, scroll to the very bottom and change Generate Serialization Assembly to Off

Good luck!