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

44,332

Solution 1

My problem was finally solved by this page - http://aplocher.wordpress.com/2012/10/12/sgen-an-attempt-was-made-to-load-an-assembly-with-an-incorrect-format-tfs-2010/

Just in case that page ever disappears in the future, here are the steps involved -

  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.

Solution 2

I encountered this same issue today. A project would not build on my PC but built fine on other PC's

I eventually fixed it by doing the following:

Right-clicked the project with the error, went into Properties

Selected the Build tab and went to the last option which is "Generate serialization assembly" I set this to Off and the project now builds fine.

Solution 3

The problem disappears after installing the latest Windows SDK which includes the 64Bit version of sgen.exe:

http://msdn.microsoft.com/en-us/windows/desktop/bg162891.aspx

Sometimes (if that one does not help) the older version helps:

http://msdn.microsoft.com/en-us/windows/desktop/hh852363.aspx

For some reason the 64bit version of sgen is not included in the Microsoft Build Tools

Solution 4

I found this issue relevant: https://github.com/dotnet/sdk/issues/1630

While waiting for this to be fixed in a future version, I was able to solve the problem by adding two targets to the csproj file, as suggested by https://github.com/joperezr:

<Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies">
    <ItemGroup>
    <ReferencePath Remove="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." />
</Target>

<Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies">
    <ItemGroup>
    <ReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" />
    </ItemGroup>
    <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has run." />
</Target>

Solution 5

I encountered the same error when I tried to compile my project (Platform target is set to x86) in Release. It compiled fine in Debug. I came to find out that in Release, Generate serialization assembly is run; hence, the call to the SGen utility. The problem was that MSBuild called the x64 version of SGen against my x86 EXE, which generated the error. I had to pass this MSBuild argument so that MSBuild uses the correct version of SGen:

/p:SGenToolPath="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools"
Share:
44,332
Paul McLean
Author by

Paul McLean

What is there to say, really?

Updated on July 09, 2022

Comments

  • Paul McLean
    Paul McLean almost 2 years

    I have a project that can build fine on my local machine, however, when I get TFS to build it, I receive the following error -

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

    After reading through many other posts here on this topic, most people just say I need to change the build type to either x86 or Any CPU, rather than x64, but after trying countless combinations, this was not the solution. My program is also a windows service, so setting the App Pool to allow 32 bit applications (as suggested by others) is also not the solution.

  • mstaffeld
    mstaffeld over 10 years
    Worked for me with an older solution and TFS2012. I ended up creating a legacy build template for the problematic solutions.
  • João Portela
    João Portela about 10 years
    My debug build was fine, but my release build was broken (even though they were both using the same process file); doing this fixed it, thanks.
  • Dave
    Dave over 9 years
    This was the solution that worked for me when my batch file was failing after adding a x32 assembly.
  • Micteu
    Micteu over 7 years
    Thank you for including the steps involved; that WordPress blog is no longer available.
  • Der_Meister
    Der_Meister over 7 years
    The same for me. I installed Microsoft Build Tools 2015. It fixed the reference assemblies and sgen issues.
  • James White
    James White over 6 years
    .NET Standard - I got this building Debug/AnyCPU with assembly that had "Generate serialization assembly = ON" and a reference to a standard assembly. When I changed it to "Auto" the error went away.
  • deadlydog
    deadlydog about 6 years
    Which component specifically do I need to install? The latest Windows SDK has 15 different components totaling 2.7GB. I'd rather only install the required component on all of my build servers. I tried installing just the .Net Framework 4.7.1 Software Development Kit, but still get the error :(
  • Louis Somers
    Louis Somers about 6 years
    @deadlydog I have no idea which components are needed. Would be great if you could single it out and post the answer here.
  • Austin Salgat
    Austin Salgat about 6 years
    This is what fixed it for us (when Debug worked but Release didn't).
  • Quango
    Quango about 6 years
    This worked for me when I built locally but when I ran on my build server this didn't fix the issue. However, I also added the SGenToolPath fix from @TDN (above) and it finally built correctly. So try this option as well!
  • kingfleur
    kingfleur about 6 years
    How did you know which version of SGEN was called?
  • TNV
    TNV about 6 years
    I think I was able to see from the build output. Sorry, it's been awhile!
  • kingfleur
    kingfleur about 6 years
    Ah yes, if you scroll up a bit, you can see it.
  • Harry
    Harry almost 6 years
    Thanks this was the most helpful answer for me. However I will point out that my release in TFS would not build on x86 so I changed my build definition to use x64. I know I added a new 32-bit dll to my solution. Do you know why it built with x64 and did not build with x86 with the SGEN error?
  • Keith Robertson
    Keith Robertson almost 6 years
    I have the opposite problem. My platform target is x64, but VS and MSBuild insist on using the x86 tools (like SGen).
  • Percy
    Percy about 5 years
    I ran my project - it worked fine. I ran it 10 minutes later and got this error - your solution worked for me - but what are the side effects of doing turning "Generate serialization assembly off"?
  • Dave Oakley
    Dave Oakley about 5 years
    As far as I know, turning it On will speed up start up time, especially if you use [serializable] attributes, as the compiler will generate a serialization assembly at compile time if this setting is turned on. Essentially it invokes SGen to generate serialized assemblies in advance, which can then be deployed with the Application. This prevents the XmlSerializer having to generate these each time the application starts.
  • toebens
    toebens over 4 years
    using 4.7.2 having the targets in csproj and also using SGenToolPath did not work
  • mhenry1384
    mhenry1384 over 4 years
    I got the current path out of the bulid log and appended /x64, which in my case made it /p:SGenToolPath="C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\x64"
  • Kirk Kuykendall
    Kirk Kuykendall over 4 years
    It has disappeared, unless you go wayback web.archive.org/web/20130208032854/http://…
  • user1034912
    user1034912 over 3 years
    This magically worked for me. Thanks! But what the hell did I do?
  • Nawar
    Nawar over 3 years
    Your answer really saved the day. I had to extend it a bit - stackoverflow.com/a/64474637/80002. In case someone faces the same problem.
  • Murphybro2
    Murphybro2 about 3 years
    Not all heroes wear capes!
  • Jettero
    Jettero about 2 years
    I set this to 'Auto' - and the project now builds fine! Thx!