14

I was able to find several similar questions asked and answered, but none of the answers or exact conditions applied to my situation.

  1. I have a .NET 3.5 project being built for x64.
  2. I also have an x64 mixed mode reference (also targeting .NET 3.5).
  3. In Visual Studio 2008, I created a Web Reference which causes SGEN to execute during a Release build to create the helper DLL.

If I don't reference the mixed-mode DLL, this works fine. If I do reference the mixed mode DLL, I get this error (xxx is just a placeholder):

SGEN : error : An attempt was made to load an assembly with an incorrect format: C:\code\xxx\trunk\xxx\common\xxx\build\winx64\lib\xxx.dll.

This is the command being issued:

C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\sgen.exe /assembly:C:\code\xxx\trunk\xxx\xxx\obj\x64\Release\xxx.dll /proxytypes /reference:..\common\xxx\build\winx64\lib\xxx.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.configuration.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Configuration.Install.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.EnterpriseServices.dll /reference:C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.Runtime.Serialization.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.ServiceModel.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.5\System.Web.Extensions.dll" /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Web.Services.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll

Any ideas?

Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
Anthony
  • 245
  • 1
  • 2
  • 10

4 Answers4

34

Have you tried changing Generate serialization assembly to Off, as suggested in this msdn post? In my case, that was the ticket.

Just go to your Project properties, and it's under Build.

Project Properties - Build

Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
  • 1
    Works for me , Sadly I don't know the impact is could cause me – Tom Stickel Jun 06 '16 at 17:39
  • `Sgen` is tightly connected to [System.Xml.Serialization.XmlSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer), it generates code for any class decorated with the `[serializable]` attribute and pre-compiles it so that less reflection is needed during runtime. So if you use `XmlSerializer` or any Soap WebService client or server then this will have a significant negative impact on performance. To be clear, when using `Xdocument` (Linq) or `XmlDocument` (System.Xml) it will make no difference at all, but `XmlSerializer` will slow down significantly. – Louis Somers Oct 12 '22 at 11:55
4

This error also pops up on a build machine after installing MSBuild Toolset (for Visual Studio 2013). The problem is that only the 32bit version of sgen.exe is available by default.

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

On one agent (machine) I needed to install the older version as well:

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

Louis Somers
  • 2,560
  • 3
  • 27
  • 57
3

This is an alternative to Ondrej's answer.

We just made the modification for our Release configuration in the project file (csproj) and set the SDK path to the x64 version:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    ...
    ...
    <GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
    <SDK40ToolsPath>C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64</SDK40ToolsPath>
  </PropertyGroup>
Community
  • 1
  • 1
John Allers
  • 3,052
  • 2
  • 30
  • 36
  • where are you guys making those changes? where is the file? – Greg Z. Jun 09 '15 at 18:44
  • Answering my own question - it is the project file (.scproj). Another question remains though: why the issue does not appear in debug build, but does appear in release build? – Greg Z. Jun 09 '15 at 18:56
  • 1
    @user1760329 According to this [answer](http://stackoverflow.com/a/9187464/73986), if `Generate serialization assembly` is set to `Auto`, it will be turned off for Debug and on for Release (if `XmlSerializer` is used in your code). – John Allers Jun 10 '15 at 00:40
1

While turning serialization assembly off, will solve the problem, it is just workaround - in the end you'll pay for it with slower start up time. The problem is you have to call sgen.exe from x64 SDK (if you're targeting x64). If you're using msbuild, set correct path to sgen like this:

<MSBuild Projects="$(MSBuildProjectLocation)" Targets="Build"
    Properties="....
    SGenToolPath=C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin\x64;" 
/>
Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • 4
    I am using VS to build. Everything is fine in Debug mode, but in Release mode I get this error. So, what do I change and, more importantly where? That is, where is the file? – Greg Z. Jun 09 '15 at 17:39