从 Visual Studio 2019 年将 ASP.NET Core 3.1 站点发布到 Azure 时出错

Error publishing an ASP.NET Core 3.1 site to Azure from Visual Studio 2019

我有一个预先存在的 ASP.NET Core 3.0 应用程序已成功部署到 Azure App Service(使用 AspNetCoreModuleV2模块)。将应用程序升级到(今天发布的)ASP.NET Core 3.1 后,应用程序在我的本地版本 IIS Express[=41= 上构建并正确运行].当我尝试使用(今天发布的)Visual Studio 16.4 发布到 Azure 应用服务时,我收到以下错误:

Assets file 'C:\Project\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v3.0'. Ensure that restore has run and that you have included 'netcoreapp3.0' in the TargetFrameworks for your project.

备注

  • 所有 <PackageReference>Microsoft.AspNetCoreMicrosoft.EntityFrameworkCoreMicrosoft.Extensions 已更新为 3.1.0
  • 我已经清理了我的解决方案,甚至核对了我的 obj 文件夹以确保没有任何挥之不去的引用。
  • 此错误是由 Microsoft.PackageDependencyResolution.targets3.1.100 版本生成的。

我知道 某些东西 仍然挂在 .NET Core 3.0 依赖项上。但不清楚为什么这只会在部署期间引起问题。 Azure App Service 的 是否还没有为 .NET Core 3.1 做好准备?或者这是依赖项解析目标的问题?

最直接的问题——如原始问题中所指出的——与设置 <TargetFramework> 的两个地方有关:

  1. 项目文件(例如,csproj
  2. 发布配置文件(即pubxml

必须在两个 位置更新<TargetFramework>,并且它们必须完全匹配。否则,发布将无法在 project.assets.json 文件中找到它的目标,该文件是基于 csjproj 文件中的 <TargetFramework> 构建的。

Note: You may well expect the pubxml file to defer to the <TargetFramework> set in the csproj file, but that is not the case.

文本编辑器

要通过文本编辑器进行修改,

  1. 打开 ~/Properties/PublishProfiles 文件夹。
  2. 打开您要编辑的 *.pubxml
  3. <TargetFramework>的值修改为netcoreapp3.1:
<TargetFramework>netcoreapp3.1</TargetFramework>

Visual Studio 2019

通过 Visual Studio 2019 IDE,

进行修改
  1. 单击 Web 一键发布 工具栏上的齿轮图标(它位于发布图标的右侧)。
  2. 假设目标框架设置为netcoreapp3.1,单击它旁边的编辑图标。
  3. 确保目标框架设置为netcoreapp3.1
  4. 单击保存

Warning: When using the IDE, you may run into a problem here. When editing the profile you'll likely see the new value from your project file (i.e., netcoreapp3.1) already selected. When you click Save, however, it will revert back to the original value (e.g., netcoreapp3.0 in my case). This is because you didn't actually change the value in the interface, which Visual Studio mistakes for there not being a change to the underlying values. If you temporarily toggle another value (e.g., Configuration), then Visual Studio will recognize that a change has occurred, and both values will be updated in the *.pubxml file.

再次感谢@PanagiotisKanavos 为我指明了正确的方向(请参阅原始线程的评论)。

改变

<PackageReferenceInclude="Microsoft.AspNetCore"Version="2.2.0" />
 to 
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />

适合我。

打开项目文件夹;

  • 导航到文件夹属性>>PublishProfiles
  • 打开文件 FolderProfile.pubxml 然后将版本 3.0 更改为 3.1

    netcoreapp3.1

  • 最后,在发布之前重建您的应用程序

我在使用 VS 网络发布器时从 VS2019(ASP.NET 核心 Web 应用程序模板)中的一个全新的 net5.0 项目得到了这个错误。解决方法如下:

  1. 打开文件:{project}\Properties\PublishProfiles\{project} - Web Deploy.pubxml

  2. <PropertyGroup> 元素内添加以下行:

    <TargetFramework>net5.0</TargetFramework>

元素完全丢失 - 干得好 MS