【问题标题】:error NETSDK1032: The RuntimeIdentifier platform 'win-x86' and the PlatformTarget 'x64' must be compatible错误 NETSDK1032:RuntimeIdentifier 平台 'win-x86' 和 PlatformTarget 'x64' 必须兼容
【发布时间】:2022-01-25 23:21:24
【问题描述】:

我的解决方案是:

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Core31ConsoleApp", "Core31ConsoleApp\Core31ConsoleApp.csproj", "{06477443-295A-47CA-A36E-A3F6B3AE47AC}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|x64 = Debug|x64
        Debug|x86 = Debug|x86
        Release|x64 = Release|x64
        Release|x86 = Release|x86
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Debug|x64.ActiveCfg = Debug|x64
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Debug|x64.Build.0 = Debug|x64
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Debug|x86.ActiveCfg = Debug|x86
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Debug|x86.Build.0 = Debug|x86
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Release|x64.ActiveCfg = Release|x64
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Release|x64.Build.0 = Release|x64
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Release|x86.ActiveCfg = Release|x86
        {06477443-295A-47CA-A36E-A3F6B3AE47AC}.Release|x86.Build.0 = Release|x86
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
    GlobalSection(ExtensibilityGlobals) = postSolution
        SolutionGuid = {17BBBF08-C7F4-4F6A-AD8F-32F2F3F21D27}
    EndGlobalSection
EndGlobal

控制台项目是:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Platforms>x86;x64</Platforms>
  </PropertyGroup>

</Project>

此项目仅包含一个包含其默认内容的 C#“Program.cs”文件:


using System;

namespace Core31ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

如您所见,它或多或少是为 .Net Core 3.1 创建的新控制台应用程序。我所做的唯一修改是用 x86 和 x64 替换了“任何 CPU”架构。可以使用 VS 成功构建 Release/Debug 和 x86/x64 的所有组合。

当我从 cmd/powershell 尝试以下两个命令中的任何一个时,它也会成功构建。

dotnet build Core31ConsoleApp.sln --configuration Debug --runtime win-x64
dotnet build Core31ConsoleApp.sln --configuration Release --runtime win-x64

当我尝试这两个之一时,问题就开始了:

dotnet build Core31ConsoleApp.sln --configuration Release --runtime win-x86
dotnet build Core31ConsoleApp.sln --configuration Debug --runtime win-x86

它们失败并出现以下错误消息:

C:\Program Files\dotnet\sdk\3.1.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): error NETSDK1032: The RuntimeIdentifier platform 'win-x86 ' 和 PlatformTarget 'x64' 必须兼容。

这是重现此问题的最小解决方案,但我有一个更大且配置相同的解决方案,但失败的方式有点不同:

C:\Program Files\dotnet\sdk\3.1.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(151,5): error NETSDK1032: The RuntimeIdentifier platform 'win-x64 ' 并且 PlatformTarget 'x86' 必须兼容。**

前两个命令失败,第二对有效。这可能是因为较大的解决方案在本节中有不同的顺序:

GlobalSection(SolutionConfigurationPlatforms) = preSolution
    Debug|x64 = Debug|x64
    Debug|x86 = Debug|x86
    Release|x64 = Release|x64
    Release|x86 = Release|x86
EndGlobalSection

我正在使用在最近的 VS 更新之一期间安装了 VS2019 和 .Net Core 3.1 SDK(64 位)的 Win 10 Prof 机器(英特尔的 I7)。系统和安装的所有软件都是最新的。

我完全不知道这里出了什么问题或者我做错了什么。

如果有任何帮助,我将不胜感激。

【问题讨论】:

    标签: .net-core


    【解决方案1】:

    向节点“RuntimeIdentifier”添加值没有用。让我问你,你是如何加载命令提示符的?您的任务栏或图标是否使用了 BATCH 文件?有没有用过

    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat
    

    如果是这样,您可能正在使用由该批处理文件创建的 C++ 命令环境运行“dotnet build”。

    回答:不要使用 VC++ 命令环境来运行 ASP.NET Core CLI。相反,请使用为 ASP.NET Core CLI 命令正确配置的命令环境。我怎么知道这个?如果您查看 vcvarsall.bat,您将在该批处理文件的第 71 到 75 行看到以下问题:

    @REM This temporary environment variable is used to control setting of VC++ 
    @REM command prompt-specific environment variables that should not be set
    @REM by the VS Developer Command prompt (specifically vsdevcmd\ext\vcvars.bat). 
    @REM The known case this effects is the Platform environment variable, which 
    @REM will override platform target for .NET builds.
    

    改为使用 VS2019 附带的那个,就像在 Windows 开始菜单中一样。命令窗口应如下所示:

    如下所示,我编译了一个WASM,只要你有正确的MS Build环境,它就可以工作,在VS 2019命令环境中设置正确:

    【讨论】:

    • 对于 VS 2019 CLI,命令行窗口将正确执行 dotnet rundotnet build 函数。首次使用后,您还会从窗口中看到一个不错的“欢迎”。它看起来像这样:Welcome to .NET 5.0! 感谢 Scott Hanselman 的独立注入,感谢 Microsoft 为我们提供了代码选项!斯科特,照顾好自己,愿你每次都正确地争论参数!
    【解决方案2】:

    由于这是一个可执行文件,请尝试在 TargetFramework 属性组中添加 &lt;RuntimeIdentifier&gt;win&lt;/RuntimeIdentifier&gt;

      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <RuntimeIdentifier>win</RuntimeIdentifier>  // add this
        <Platforms>x86;x64</Platforms>
      </PropertyGroup>
    

    【讨论】:

      【解决方案3】:

      我的错误消息不同,但我的修复可能适用。错误是:

      The RuntimeIdentifier platform 'browser-wasm' and the PlatformTarget 'x64' must be compatible.
      

      该项目是 Blazor WebAssembly 项目。 csproj 文件中有这个:

        <PropertyGroup>
          <TargetFramework>net5.0</TargetFramework>
        </PropertyGroup>
        <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
          <PlatformTarget>x64</PlatformTarget>
        </PropertyGroup>
      

      我用 anycpu 替换了 x64,这是我在用 browser-wasm 替换 x64 后收到的另一条错误消息。哈。

      在修复之前,我在解决方案资源管理器中的所有引用/依赖项上都有警告三角形。标记说的是,我不知道 DxDataGrid 是什么。你需要@using 语句吗?修复后,这一切都消失了。

      【讨论】:

        【解决方案4】:

        项目设置 -> 生成 -> 目标平台 -> AnyCPU -> 保存!好的!

        dotnet publish -r win-x86 -c Release --self-contained

        dotnet publish -r win-x64 -c Release --self-contained

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多