构建 ASPNetCore 6.0 项目:Microsoft 包与 .NETFramework,Version=v6.0 不兼容,但支持 .NETCoreApp,Version=v6.0

Building ASPNetCore 6.0 project: Microsoft packages not compatible with .NETFramework,Version=v6.0, but support .NETCoreApp,Version=v6.0 instead

我使用 Visual Studio 2022 (17.0.4) 构建了一个 ASPNetCore 6.0 网站,在我的开发机器上一切正常。

我现在正尝试通过 Jenkins 构建服务器部署它。我首先通过 https://dotnet.microsoft.com/en-us/download/dotnet/6.0 安装了 .Net 6.0.2 SDK 和 ASP.NET Core Runtime 6.0.2 托管包。在 Hosting Bundle 之后是否按照要求重新启动。

我现在 运行 命令 dotnet publish /p:Configuration=Release .\path\to\my.csproj /p:PublishProfile=FolderProfile 我遇到了一堆错误,例如这个:

error NU1202: Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 6.0.2 supports: net6.0 .NETCoreApp,Version=v6.0)

error NU1202: Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0)

包Microsoft.EntityFrameworkCore.Design 6.0.2、Microsoft.EntityFrameworkCore.Proxies 6.0.2、Microsoft.EntityFrameworkCore.Relational 6.0.2、Microsoft.EntityFrameworkCore.SqlServer 6.0.2、Microsoft.EntityFrameworkCore.Tools6.0.2

我不明白为什么它告诉我它似乎已经下载了支持 .NETCoreApp,Version=v6.0 的包,但想要与 .NETFramework,Version=v6.0 兼容的包。我认为 .NET 6.0 的重点是 Core 和 Framework 之间不再有任何区别,它们都是一回事?

为确保我 运行 的工具版本正确(检查 处的答案),dotnet --version 给出 6.0.200。当 运行 dotnet publish 它说的第一件事是 Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET.

解决方案中的所有项目都是 SDK 样式(因为在 .csproj 文件中它们包含 <Project Sdk="Microsoft.NET.Sdk.Web"><Project Sdk="Microsoft.NET.Sdk">,并且所有目标都是 net6.0 <TargetFramework>net6.0</TargetFramework>.

关于如何解决这个问题的任何想法?

编辑:我注意到一些包给出了稍微不同的错误,这是完整的错误列表(我已经删除了最初包含在每行开头的项目路径):

error NU1202: Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Design 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.EntityFrameworkCore.Design 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Proxies 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0).Package Microsoft.EntityFrameworkCore.Proxies 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Relational 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.EntityFrameworkCore.Relational 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.SqlServer 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.EntityFrameworkCore.SqlServer 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Tools 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.EntityFrameworkCore.Tools 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0). Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Design 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.EntityFrameworkCore.Design 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Proxies 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.EntityFrameworkCore.Proxies 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Relational 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.EntityFrameworkCore.Relational 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.SqlServer 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.EntityFrameworkCore.SqlServer 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.EntityFrameworkCore.Tools 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.EntityFrameworkCore.Tools 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0) error NU1202: Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 6.0.2 is not compatible with net60 (.NETFramework,Version=v6.0) / win7-x86. Package Microsoft.AspNetCore.Identity.EntityFrameworkCore 6.0.2 supports: net6.0 (.NETCoreApp,Version=v6.0)

在此列表后半部分的错误中引用了“win7-x86”。我不知道为什么,这是 64 位 OS 上的 Windows Server 2012 服务器 运行,我已经安装了 64 位版本的 SDK(在 Programs and功能)。

好吧,折腾了好久,终于解决了。我做了很多事情,所以我不确定什么是真正必要的,什么不是,但我会在这里详细说明这些步骤。

首先,我在服务器上安装了 3 个 .NET 核心 SDK,包括 6.0.200。我卸载了所有这些并重新启动。然后我再次安装 6.0.200 并重新启动。

此时我再次尝试重建并得到了一个不同的错误:MSB4236: The SDK 'Microsoft.NET.Sdk.Web' specified could not be found - 这很奇怪,因为 SDK 确实安装在正确的位置等等。

这让我找到了 https://github.com/dotnet/sdk/issues/10229 and https://github.com/dotnet/msbuild/issues/2532,这让我创建了一个指向 C:\Program Files\dotnet\sdk.0.200\Sdks 的新环境变量 MSBuildSDKsPath,它又重新启动了一次,让 Jenkins 接受了它,但在那之后,构建开始工作:)