Forcing VS2022 to use 32 bit version of msbuild

I'm currently investigating migrating our toolset from VS2013, 15, and 17 to just VS2022 in order to streamline and reduce the amount of software needed to build our solution. VS2022 has all the parts we need however I'm struggling to get the actual solution to build through VS itself.

The solution builds entirely fine if I use the accompanying 32 bit version of msbuild at the path C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\MSBuild.exe. However the C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\amd64\MSBuild.exe version (which I'm guessing VS22 uses?) fails to build with the same errors as through VS22.

It's important to note our solution/projects are primarily based on .net framework 2.0 (unfortunately) and are set to x86 platform.

I keep cycling back to the following error SGEN : error : An attempt was made to load an assembly with an incorrect format on various first and third-party dll's. I've been trialling back and forth combinations of the following:

  • changing projects between x86 and AnyCPU but the error still crops regardless of the project and it's references' configurations
  • turning off Generate serialization assembly on the projects as I've seen somewhere that people have had success solving the error by doing this.
  • changing ToolsVersion from 4.0 (our current setting) to Current and other versions

I'm effectively looking for a way to force VS2022 to use the 32bit msbuild if that's at all possible as it builds the entire solution without making any changes at all.

I did also notice the following in VS output window:

Task attempted to find "sgen.exe" using the SdkToolsPath value "". Make sure the SdkToolsPath is set to the correct value and the tool exists in the correct processor specific location below it. (TaskId:109)
4>  C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\sgen.exe

Would setting the SdkToolsPath to the 32 bit version solve my problem? I haven't been actually been able to find where to do this to try it.

Thank you!


Solution 1:

What has worked for us is overriding the SGenToolPath project property manually in the .csproj files

Add the followings:

<Target Name="SGENBeforeBuild" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <!--  workaround for VS2022 x64 building serialization assemblies for x86 targets
        , see https://stackoverflow.com/questions/70770918/forcing-vs2022-to-use-32-bit-version-of-msbuild -->
    <SGenToolPath>$(TargetFrameworkSDKToolsDirectory)</SGenToolPath>
  </PropertyGroup>
</Target>

Interestingly, we had to use this workaround for some x86 targeting projects, not all of them.

In fact, you might (we did) run into a similar problem with a number of Visual Studio build tool steps. Here's an MSDN page that lists project properties for most Visual Studio (2022) build tools:
https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties?view=vs-2022