3

我目前正在研究将我们的工具集从 VS2013、15 和 17 迁移到 VS2022,以简化和减少构建我们的解决方案所需的软件数量。VS2022 拥有我们需要的所有部件,但是我正在努力获得通过 VS 本身构建的实际解决方案。

如果我在路径中使用附带的 32 位版本的 msbuild,则该解决方案构建得非常好C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\MSBuild.exe。但是该C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Current\Bin\amd64\MSBuild.exe版本(我猜是 VS22 使用的?)无法构建与通过 VS22 相同的错误。

重要的是要注意我们的解决方案/项目主要基于.net framework 2.0(不幸的是)并设置为x86平台。

我不断循环回到SGEN : error : An attempt was made to load an assembly with an incorrect format各种第一方和第三方 dll 上的以下错误。我一直在尝试以下组合:

  • 在和之间更改项目x86AnyCPU但无论项目及其参考配置如何,错误仍然会出现
  • 关闭Generate serialization assembly项目,因为我在某处看到人们通过这样做已经成功解决了错误。
  • 从(我们当前的设置)更改为ToolsVersion和其他版本4.0Current

如果可能的话,我正在有效地寻找一种强制 VS2022 使用 32 位 msbuild 的方法,因为它构建了整个解决方案而根本不做任何更改。

我还注意到 VS 输出窗口中的以下内容:

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

设置SdkToolsPath为 32 位版本会解决我的问题吗?我实际上一直无法找到在哪里进行此尝试。

谢谢!

4

1 回答 1

2

对我们有用的是SGenToolPath在 .csproj 文件中手动覆盖项目属性

添加以下内容:

<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>

有趣的是,我们不得不对一些x86 目标项目使用这个解决方法,而不是所有项目。

事实上,您可能(我们确实)在许多 Visual Studio 构建工具步骤中遇到了类似的问题。这是一个 MSDN 页面,其中列出了大多数 Visual Studio (2022) 构建工具的项目属性:
https ://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties?view=vs-2022

于 2022-02-01T09:39:52.647 回答