Azure DevOps:资产文件没有“.NETCoreApp,Version=v2.2/linux-x64”的目标

swd*_*don 4 nuget-package-restore azure-devops azure-pipelines-release-pipeline

我正在使用任务组在 Azure DevOps 中为 AspNet Core Web Api 创建一个构建管道,该任务组首先安装 .Net Core SDK (2.2),然后使用dotnet restore(使用 VSTS 源)进行包还原,然后构建项目。

在构建步骤,我提供以下参数:

--configuration Release --runtime $(Target Platform) --no-restore /p:Version=$(Major Version).$(Minor Version).$(Build.BuildNumber).

buildstep 的所有步骤都成功了。但是,我在构建步骤中收到以下错误:

[command]C:\windows\system32\chcp.com 65001
Active code page: 65001
[command]C:\hostedtoolcache\windows\dotnet\dotnet.exe build D:\a\1\s\WebApp\WebApp.csproj --configuration Release --runtime linux-x64 --no-restore /p:Version=1.0.40
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

C:\hostedtoolcache\windows\dotnet\sdk\2.2.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208,5): error NETSDK1047: Assets file 'D:\a\1\s\WebApp\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.2/linux-x64'. Ensure that restore has run and that you have included 'netcoreapp2.2' in the TargetFrameworks for your project. You may also need to include 'linux-x64' in your project's RuntimeIdentifiers. [D:\a\1\s\WebApp\WebApp.csproj]

Build FAILED.

C:\hostedtoolcache\windows\dotnet\sdk\2.2.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208,5): error NETSDK1047: Assets file 'D:\a\1\s\WebApp\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.2/linux-x64'. Ensure that restore has run and that you have included 'netcoreapp2.2' in the TargetFrameworks for your project. You may also need to include 'linux-x64' in your project's RuntimeIdentifiers. [D:\a\1\s\WebApp\WebApp.csproj]
    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.53
##[error]Error: The process 'C:\hostedtoolcache\windows\dotnet\dotnet.exe' failed with exit code 1
##[error]Dotnet command failed with non-zero exit code on the following projects : D:\a\1\s\WebApp\WebApp.csproj
##[section]Finishing: Build
Run Code Online (Sandbox Code Playgroud)

但是,我查看了与 Visual Studio 相关的旧答案,但找不到与 Azure DevOps 管道相关的内容。

Mer*_*SFT 5

要解决此问题,请尝试删除--no-restore.

为了解释为什么它在本地没有问题,那是因为当您在本地构建项目时,如果当前文件不满足编译条件,它将使用缓存中存在的先前版本的obj文件。您可以尝试在本地删除binobj文件,然后再次运行这些命令。您还将看到相同的错误。这是我在本地遇到的错误,并通过删除--no-restore.

默认情况下,如果您没有指定--runtime linux-x64,则它恢复的包的 RID 是win-x64。您可以使用自托管代理在 Azure Devops 中仅运行dotnet restore任务进行测试,并检查 project.assets.json 文件:

在此处输入图片说明

您将看到,如果没有指定,它恢复的包的默认 RID 是win-x64. 与dotnet build任务相同,因为从 .NET Core 2.0 SDK 开始,dotnet restore 在您运行 dotnet build 时隐式运行

在此处输入图片说明

在这个时候,如果你指定--no-restoredotnet build,因为默认情况下,它会使用它恢复被任务“DOTNET恢复”,这RID是包win-x64但是在您的构建任务中,您将runtime构建指定为linux-x64. 这与包裹不符。这就是您收到消息的原因:error NETSDK1047: Assets file 'D:\a\1\s\WebApp\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.2/linux-x64'.

只需删除并重--no-restore

在此处输入图片说明

错误信息应该消失了。