构建 .NET 5.0 项目 Azure DevOps 管道

Posted

技术标签:

【中文标题】构建 .NET 5.0 项目 Azure DevOps 管道【英文标题】:Building .NET 5.0 project Azure DevOps pipeline 【发布时间】:2020-11-14 02:58:41 【问题描述】:

我正在尝试使用 Azure DevOps 管道构建在 .NET 5.0 中构建项目,但收到此错误

2020-11-14T01:59:45.8238544Z [command]"C:\Program Files\dotnet\dotnet.exe" build D:\a\1\s\XXX.csproj "-dl:CentralLogger,\"D:\a\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.178.0\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\"*ForwardingLogger,\"D:\a\_tasks\DotNetCoreCLI_5541a522-603c-47ad-91fc-a4b1d163081b\2.178.0\dotnet-build-helpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll\""
2020-11-14T01:59:46.1472016Z Microsoft (R) Build Engine version 16.7.0+7fb82e5b2 for .NET
2020-11-14T01:59:46.1473316Z Copyright (C) Microsoft Corporation. All rights reserved.
2020-11-14T01:59:46.1473902Z 
2020-11-14T01:59:46.6006398Z   Determining projects to restore...
2020-11-14T01:59:47.2059773Z   Restored D:\a\1\s\XXX.csproj (in 234 ms).
2020-11-14T01:59:47.2119638Z   1 of 2 projects are up-to-date for restore.

    2020-11-14T01:59:47.3209350Z ##[error]C:\Program Files\dotnet\sdk\3.1.403\Microsoft.Common.CurrentVersion.targets(1177,5): Error MSB3644: The reference assemblies for .NETFramework,Version=v5.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks

2020-11-14T01:59:47.3261839Z C:\Program Files\dotnet\sdk\3.1.403\Microsoft.Common.CurrentVersion.targets(1177,5): error MSB3644: The reference assemblies for .NETFramework,Version=v5.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [D:\a\1\s\XXX.csproj]
2020-11-14T01:59:47.3270768Z 
2020-11-14T01:59:47.3274231Z Build FAILED.
2020-11-14T01:59:47.3275925Z 
2020-11-14T01:59:47.3277393Z C:\Program Files\dotnet\sdk\3.1.403\Microsoft.Common.CurrentVersion.targets(1177,5): error MSB3644: The reference assemblies for .NETFramework,Version=v5.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [D:\a\1\s\XXX.csproj]
2020-11-14T01:59:47.3279484Z     0 Warning(s)
2020-11-14T01:59:47.3279860Z     1 Error(s)
2020-11-14T01:59:47.3280170Z 
2020-11-14T01:59:47.3280537Z Time Elapsed 00:00:01.09
2020-11-14T01:59:47.3624731Z ##[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1

有人知道 Azure DevOps 管道是否支持构建 .NET 5.0 代码?

【问题讨论】:

【参考方案1】:

是的,Azure DevOps Pipelines 可以构建 net5.0 应用程序。

如果您使用“.Net Core”(yaml 中的DotNetCoreCLI)任务进行构建 - 添加“Use .NET Core”(yaml 中的UseDotNet)之前的任务,版本正确:

- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '5.0.x'

- task: DotNetCoreCLI@2
  displayName: 'dotnet build'
  inputs:
    command: 'build'

【讨论】:

【参考方案2】:

支持。

由于您使用的是 .Net 5,而不是使用 Nuget 还原,请尝试将 Use .net core taskDotnet core task 与还原命令一起使用。

- task: UseDotNet@2
  displayName: 'Use .NET Core sdk 5.0.100'
  inputs:
    packageType: 'sdk'
    version: '5.0.100'
    includePreviewVersions: true

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore'
  inputs:
    command: restore
    projects: '**/*.csproj'

强烈建议对以.net core 为目标的项目使用dotnet restoredotnet build 任务。见this statement from Nuget task:

也可以在这里查看类似的问题:Azure CI pipeline for Blazor .NET 5 doesn't work

【讨论】:

nuget restore 换成dotnet restore 为我解决了这个问题。【参考方案3】:

我需要同时使用两个框架版本来在 .net 5 中构建我的功能

步骤:

- task: UseDotNet@2
  inputs:
    version: '5.0.x'
    packageType: sdk
    includePreviewVersions: false

- task: UseDotNet@2
  inputs:
    version: '3.1.x'
    packageType: sdk
    includePreviewVersions: false

【讨论】:

以上是关于构建 .NET 5.0 项目 Azure DevOps 管道的主要内容,如果未能解决你的问题,请参考以下文章

.NET 5.0:在多项目解决方案中构建项目

从 3.1 升级后的 .NET Core 5.0 Azure 部署 CORS 问题

如何在 Azure DevOps Server 2020 中为 C#10 / .NET 6.0 项目创建构建任务

Microsoft Graph API - Blazor Server App .NET 5.0 使用 Azure 身份验证,MsalUiRequiredException 因用户质询而引发

从 Azure 中托管的 ASP.NET Core 5.0 MVC 站点调用 API/服务的间歇性套接字异常

Blazor 项目在本地构建,但不在 azure 管道中