Azure Pipeline - 使用 .NET 8 RC2 SDK 构建 C# 项目失败

Ana*_*ran 4 .net azure .net-core azure-functions azure-pipelines

我们有一个 Azure Function 应用程序项目,它可以使用 .NET 8 RC2 SDK 在本地构建良好。但是,当通过 Azure Pipeline 进行构建时,在恢复 NuGet 包步骤期间会失败,并引发以下错误:

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at NuGet.CommandLine.RestoreCommand.<DetermineInputsFromMSBuildAsync>d__63.MoveNext()
##[error]The nuget command failed with exit code(1) and error(D:\a\1\s\some.API.csproj : error : Could not resolve SDK "Microsoft.NET.Sdk". 
Exactly one of the probing messages below indicates why we could not resolve the SDK. Investigate and resolve that message to correctly specify the SDK.

D:\a\1\s\SOME.API.csproj : error :   Unable to locate the .NET SDK. Check that it is installed, your PATH is configured for the correct architecture, and that the version specified in global.json (if any) matches the installed version.

D:\a\1\s\SOME.API.csproj : error :   The NuGetSdkResolver did not resolve this SDK because there was no version specified in the project or global.json.

D:\a\1\s\SOME.API.csproj : error :   MSB4276: The default SDK resolver failed to resolve SDK "Microsoft.NET.Sdk" because directory "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk\Sdk" did not exist.

D:\a\1\s\SOME.API.csproj : error MSB4236: The SDK 'Microsoft.NET.Sdk' specified could not be found. No .NET SDKs were found.
Run Code Online (Sandbox Code Playgroud)

在此之前的步骤中,已成功下载 .NET 8.0.x SDK,并通过“使用 .NET Core”任务步骤的“包括预览版本”启用管道。

任何想法导致此错误的原因是什么?

Mia*_*SFT 5

当我使用 NuGet 恢复任务构建 .NET 8 项目时,我可以重现该问题。

- task: NuGetToolInstaller@1
 
- task: NuGetCommand@2
  inputs:
    restoreSolution: '**/*.sln'
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用DotNet恢复任务,这次成功了。

- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    projects: '**/*.sln'
    feedsToUse: 'select'
Run Code Online (Sandbox Code Playgroud)

查看两个任务的详细日志后,发现NuGet恢复任务中,出现了“ msBuildVersion”:“ 17.7.2.0”。在 DotNet 恢复任务中,MSBuild version=“ 17.8.0+6cdef4241”。

正如 jessehouwing 和在此宣布 .NET 8 候选版本 2中所提到的,

.NET SDK 8.0.100-rc.2 必须与 Visual Studio 17.8 Preview 3 一起使用

根据以上情况,这个问题确实与VS版本(msBuildVersion)有关。DotNet 恢复任务将在构建中使用最新的 .NET 8.0.x SDK。因此,解决方法是使用 DotNet 恢复任务而不是 NuGet 恢复任务。

更新:

在 dotnet 构建中,DotNet 恢复隐式运行(请参阅隐式恢复)。当恢复步骤成功时,您可以添加参数'--no-restore'以在构建步骤中禁用恢复。

- task: DotNetCoreCLI@2
  inputs:
    command: 'build'
    projects: '**/*.csproj'
    arguments: '--no-restore'
  name:   build
Run Code Online (Sandbox Code Playgroud)