Friday 8 July 2011

Nant script error in TeamCity - SGEN An attempt was made to load an assembly with an incorrect format

I'm using TeamCity to pull from a Mecurial repository and then execute a Nant script to build a Visual Studio solution file. The compilation fails with the following error:

"SGEN An attempt was made to load an assembly with an incorrect format"

I read various posts while using Google to find a solution. One option was to change the Generate Serialization Assembly flag to Off in the project properties build section. This did stop the error for that particular dll but it cropped up again in another project.

This was not the best solution because we are just hiding the actual error and SGEN is needed to improve performance on first run by serializing the dll.

So after a lot of head scratching I opened up the TeamCity Visual Studio solution and had a look at the Configuration Manager for my Release build. Sure enough the configuration differed in my dev Release build which targeted Any CPU and on the TeamCity server it targeted Mixed Platforms.

Resolution:

  1. Open up your dev solution
  2. Replace all lines that match this regular expression ^.*|Mixed Platforms.*$ with nothing - I use NotePad++ for this
  3. Save the solution
  4. Commit and then Push to Mecurial
  5. Run the TeamCity build once again

The reason I'm hacking the actual solution file and not changing it in Visual Studio is because there is some UI error when editing in VS that keeps the Mixed Platforms line in the solution file. BY hacking the actual file we avoid this problem.

No comments: