0

Looks like there are many similar problems without good explanation.

Environment:

Win 7

VS2012 Pro

Windows SDK 7.0

In our environment we generate 2 code bases - x86 and x64. For this specific project we generate *.XMlSerializers.dll. The setting in the build for Generate Serialization Assemblies is Auto.

When I compile project to x64 config, I get SGEN error "attempt load assembly in incorrect format... system.data.dll". This error is totally misleading because farther investigation brought the following results:

I used procmon from sysinternals to trace SGEN

  1. When building for AnyCpu and x86 procmon shows a trace. The sgen in use is from C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin (32-bit sngen.exe)
  2. When building for x64 there is no trace and only error mentioned above

I set project configuration to create serializers to OFF and compiled assembly in x64. Then I tried a command line

  1. Success came about using SGEN in C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\x64 - (please note - this is 64-bit sgen.exe)
  2. Running 32-bit SGEN on x64 Assembly failed.

These results lead to a conclusion: When VS2012 compiles a project with Generate Serialization Assemblies set to ON (auto is a special case), it attempts to find sgen.exe. But when your project configuration is set to build for x64 platform, it fails to find appropriate sgen.exe, one that itself was built for x64.

Question: Is there msbuild configuration or registry setting, or something else there, that can be set to allow Visual Studio find appropriate SGEN?

I know about workarounds, i.e. Post Build events and turning it off but this is not the point.

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • 1
    I don't have time to try at the moment, but have you attempted to use the `Platform` attribute of the [`SGen task`](https://msdn.microsoft.com/en-us/library/vstudio/ms164303(v=vs.110).aspx)? Also, somewhat related: http://stackoverflow.com/q/2748974/239394. – Andrew Mar 26 '15 at 22:41
  • @Andrew You may be onto something. I'll have to try and research – T.S. Mar 27 '15 at 00:37
  • http://www.codeproject.com/Articles/526956/All-about-XmlSerializer-Performance-and-Sgen – T.S. Mar 29 '15 at 20:06

2 Answers2

0

Generally speaking, one could add this XML into specific configuration in the project file, and this will point to specific sgen.exe that you want.

<SGenToolPath>C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\x64</SGenToolPath>

But to solve my issue in a broader, environment-specific way I had to set Generate Serialization Assembly to OFF and use separate command line action post-build

"path to your specific\sgen.exe" "path\yourFile.dll" /f

I had to do it this way because many developers here use different versions of VS studio, etc. They set their machines and who knows what and where they install. So, I added a specific action to a build process to perform this step separately because I know exactly where specific sgen.exe is located on build server.

T.S.
  • 18,195
  • 11
  • 58
  • 78
0

We require the xml serializer to be built while compiling, so there is no option for us to not use create serialization task.

But this issue it is not related with the location of Sgen.exe but with the references path build while executing msbuild target GetReferenceAssemblyPaths

we finally solved the issue appending a new target after csharp targets import in each .csproj file

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

Next xml fragment solved the issue:

<Target Name="Prependx64FrameworkPath" Condition="'$(Platform)' == 'x64'" AfterTargets="GetReferenceAssemblyPaths">
    <Message Importance="normal" Text="Fixing target framework directory for x64 build before running SGen." />
    <PropertyGroup>
        <TargetFrameworkDirectory>C:\Windows\Microsoft.NET\Framework64\v2.0.50727;$(TargetFrameworkDirectory)</TargetFrameworkDirectory>
    </PropertyGroup>
   </Target>

This fragment forces the prepend of .net framework x64 path before any other, then the task that search for references will find x64 dlls before any other one.

After adding this on each .vcproj, sgen completes with success while building x64 binaries.