错误 NETSDK1045:当前的 .NET SDK 不支持面向 .NET 6.0

Error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0

我花了 2 个小时试图弄清楚我的 Azure Functions .NET6 管道有什么问题(Windows)。

Error NETSDK1045: The current .NET SDK does not support targeting .NET 6.0.  Either target .NET 5.0 or lower, or use a version of the .NET SDK that supports .NET 6.0.

我在这里找到了解决方案 https://jaliyaudagedara.blogspot.com/2021/07/azure-devops-building-projects.html
如果我指定 .NET Core SDK 版本并将预览版本设置为 true,它就会工作

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

所以我的最终管道看起来像这样

# .NET Core Function App to Windows on Azure
# Build a .NET Core function app and deploy it to Azure as a Windows function App.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/en-us/azure/devops/pipelines/languages/dotnet-core

trigger:
- master
- main
- dev

variables:
  azureSubscription: 'XXXX'
  functionAppName: 'XXXX'
  vmImageName: 'windows-latest'
  workingDirectory: '$(System.DefaultWorkingDirectory)/XXXX'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - task: UseDotNet@2
      displayName: 'Use .NET 6 Core sdk'
      inputs:
        packageType: 'sdk'
        version: '6.0.x'
        includePreviewVersions: true

    - task: DotNetCoreCLI@2
      displayName: Build
      inputs:
        command: 'build'
        projects: |
          $(workingDirectory)/*.csproj
        arguments: --output $(System.DefaultWorkingDirectory)/publish_output --configuration Release

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(System.DefaultWorkingDirectory)/publish_output'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'development'
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionApp
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

对于经典编辑器 - 只需将代理设置为 Windows 2022 并确保使用最新的 Nuget 版本(我使用的是 5.8,它运行良好)。

我在 运行 一个项目时遇到了这个错误,安装 .Net 6.0 SDK 解决了我的问题。 https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/sdk-6.0.102-windows-x64-installer

我在 VS2022 上制作的 .Net6 核心项目上 运行 流水线时遇到了这个错误。将池 vmimagewindows-latest 更改为 windows-2022 后,一切对我有用。

Reference MS Documentation

还为 Nugetinstalle 添加了最新版本 Nuget versions

steps:
 - task: NuGetToolInstaller@1
 inputs:
   versionSpec: '6.1'
   checkLatest: true

现在我的管道是这样的,

trigger:
- master

pool:
  vmImage: 'windows-2022'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1
  inputs:
    versionSpec: '6.1'
    checkLatest: true

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package     /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
platform: '$(buildPlatform)'
configuration: '$(buildConfiguration)'

已解决 : .Net6 核心项目在 VS2022.Need 上创建,以使用支持的 .NET SDK 版本.NET 6.0.

在 .NET Core 发布任务之前需要添加另一个任务(下面一个用于使用特定版本的 SDK 6.0

steps:
- task: UseDotNet@2
  displayName: 'Use .NET Core sdk 6.0.x'
  inputs:
    packageType: '$(Parameters.packageType)'
    version: 6.0.x
    includePreviewVersions: true

我完整的 YAML 看起来像(2022 年 5 月 26 日成功构建)

variables:
- name: BuildParameters.packageType
  value: sdk
resources:
  repositories:
  - repository: self
    type: git
    ref: refs/heads/Develop
jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: windows-2019
  steps:
  - checkout: self
  - task: NuGetToolInstaller@1
    displayName: 'Use NuGet '
  - task: NuGetCommand@2
    displayName: NuGet restore
    inputs:
      solution: Marielinas
  - task: UseDotNet@2
    displayName: Use .NET Core sdk 6.0.x
    inputs:
      packageType: $(BuildParameters.packageType)
      version: 6.0.x
      includePreviewVersions: true
  - task: DotNetCoreCLI@2
    displayName: dotnet publish
    inputs:
      command: publish
      arguments: -c release --output $(Build.ArtifactStagingDirectory)
      workingDirectory: Marielinas.ASP.NET6.0
  - task: AzureWebApp@1
    displayName: 'Azure Web App Deploy: marielinasshop'
    inputs:
      azureSubscription: 37f84ce6-e4f1-4a03-b107-9c9a527dff4a
      appType: webApp
      appName: marielinasshop
      deployToSlotOrASE: true
      resourceGroupName: GreenLab
      package: $(Build.ArtifactStagingDirectory)/**/*.zip
...