升级到 .net 5 后 azure devops 构建中的 nuget 还原步骤出错

Error on nuget restore step in azure devops build after upgrade to .net 5

我在 azure devops 中有一个构建管道,它适用于 net core 3.1,然后我将这个项目升级到 .net 5.0,构建不再工作,它在 nuget 恢复步骤停止

这是错误日志

 MSBuild auto-detection: using msbuild version '16.200.19.31001' from 'D:\Program Files (x86)\Microsoft Visual Studio19\Preview\MSBuild\Current\bin'. Use option -MSBuildVersion to force nuget to use a specific version of MSBuild.
MSBuild P2P timeout [ms]: 120000
D:\Program Files (x86)\Microsoft Visual Studio19\Preview\MSBuild\Current\bin\msbuild.exe "C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp\NuGetScratch\xbcsorbg.z0f.nugetinputs.targets" /t:GenerateRestoreGraphFile /nologo /nr:false /v:q /p:NuGetRestoreTargets="C:\Windows\ServiceProfiles\NetworkService\AppData\Local\Temp\NuGetScratch\p0q3isxq.jbq.nugetrestore.targets" /p:RestoreUseCustomAfterTargets="True" /p:RestoreTaskAssemblyFile="C:\agent\_work\_tool\NuGet.7.0\x64\nuget.exe" /p:RestoreSolutionDirectory="C:\agent\_work\s\Framework\" /p:RestoreConfigFile="C:\agent\_work\Nuget\tempNuGet_1947.config" /p:SolutionDir="C:\agent\_work\s\Framework\" /p:SolutionName="Tenper Core"
NuGet.CommandLine.ExitCodeException: Exception of type 'NuGet.CommandLine.ExitCodeException' was thrown.
   at NuGet.CommandLine.MsBuildUtility.<GetProjectReferencesAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.CommandLine.RestoreCommand.<GetDependencyGraphSpecAsync>d__68.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   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(C:\agent\_work\_tool\dotnet\sdk.0.100-rc.1.20452.10\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(54,5): error MSB4186: Invalid static method invocation syntax: "[MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')". Method '[MSBuild]::GetTargetFrameworkIdentifier' not found. Static method invocation should be of the form: $([FullTypeName]::Method()), e.g. $([System.IO.Path]::Combine(`a`, `b`)). Check that all parameters are defined, are of the correct type, and are specified in the right order. [C:\agent\_work\s\Framework\Interfaces\Interfaces.csproj]
C:\agent\_work\_tool\dotnet\sdk.0.100-rc.1.20452.10\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(54,5): error MSB4186: Invalid static method invocation syntax: "[MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')". Method '[MSBuild]::GetTargetFrameworkIdentifier' not found. Static method invocation should be of the form: $([FullTypeName]::Method()), e.g. $([System.IO.Path]::Combine(`a`, `b`)). Check that all parameters are defined, are of the correct type, and are specified in the right order. [C:\agent\_work\s\Framework\DI\DI.csproj]

出现该问题是因为您使用的agent不是.Net Core sdk 5.0 preview 8.

根据您的错误日志,您使用的 SDK 5.0.100-rc.1 不受支持 [MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')

如何解决这个问题:

您可以将原来的 .Net Core 下载任务替换为以下下载任务:

- task: UseDotNet@2
  inputs:
    packageType: 'sdk'
    version: '5.0.100-preview.8.20417.9'
    includePreviewVersions: true

然后重建您的管道。

尝试使用 Use .net core task+Dotnet core task 和恢复命令。

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

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

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

如果您正在使用 .NET Core.NET Standard,请使用 .NET Core task,它完全支持所有包方案,并且当前受 dotnet 支持。

另外,您应该根据错误信息在UseDotNet@2任务中使用5.0.100-rc.1.20452.10

我也有类似的问题。我将我的项目从 3.1 -> 5.0 升级,恢复选项失败。但是,我使用的是 dotnet cli 来恢复 nuget 包。

原来,有一个名为 WorkerExtensions.csproj 的项目正在恢复,并且是使用 3.1 构建的。我完全不确定这个项目在哪里被引用,因为它不在我正在构建的项目中的任何地方。

  Project.AzureFunction -> /home/vsts/work/1/s/publish_output/Project.AzureFunction.dll
  Determining projects to restore...
  Restored /tmp/o0lv1ony.pel/WorkerExtensions.csproj (in 6.89 sec).
  WorkerExtensions -> /tmp/o0lv1ony.pel/buildout/Microsoft.Azure.Functions.Worker.Extensions.dll
##[error]/home/vsts/.nuget/packages/microsoft.net.sdk.functions/3.0.11/build/Microsoft.NET.Sdk.Functions.Build.targets(32,5): Error : It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '3.1.0' was not found.
  - The following frameworks were found:
      5.0.8 at [/opt/hostedtoolcache/dotnet/shared/Microsoft.NETCore.App]

我发现唯一接近的是 Microsoft.Azure.Functions.Worker.Extensions.Http nuget。查了下还是想不通

无论如何,为了让构建工作并部署它,我在 YAML 中包含了 .NET Core 3.1 和 .NET 5,如下所示。希望以后能妥善解决这个问题。

# For some reason, Azure Functions still require .NET Core 3.x even with .NET 5. Maybe it won't be needed in the future.
    - task: UseDotNet@2 
      displayName: "Install .NET Core 3.x"
      inputs:
        version: '3.x'
        packageType: sdk

    - task: UseDotNet@2
      displayName: 'Install .NET 5.x'
      inputs:
        packageType: sdk
        version: '5.x'