【问题标题】:NETSDK1045: The current .NET SDK does not support 'newer version' as a targetNETSDK1045:当前的 .NET SDK 不支持“较新版本”作为目标
【发布时间】:2021-12-30 17:42:21
【问题描述】:

我创建了一个简单的 ASP.NET CORE 6 Web API。然后我把它推到了 Github。当我尝试在 Azure Devops 中创建管道时,出现错误。

C:\Program Files\dotnet\sdk\5.0.403\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET
   .TargetFrameworkInference.targets(141,5): 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.

我刚刚下载了 VS 2022 社区版。我已经安装了 .Net SDK 6。 这是我的 csproj 文件

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
  </ItemGroup>

</Project>

我选择了 Azure devops 的建议,所以这是我的 yml 文件

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

 trigger:
  - master

 pool:
    vmImage: 'windows-latest'

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

 steps:
    - task: NuGetToolInstaller@1

    - 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:PackageLocation="$(build.artifactStagingDirectory)"'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'

   - task: VSTest@2
       inputs:
         platform: '$(buildPlatform)'
         configuration: '$(buildConfiguration)'

我已按照 this documentation 中的说明进行操作,但它不起作用。构建仍然像这样失败

[![在此处输入图片描述][2]][2]

感谢您的帮助

【问题讨论】:

    标签: asp.net-core azure-devops azure-pipelines


    【解决方案1】:

    我选择了 Azure devops 的建议,所以这是我的 yml 文件

    不幸的是,很容易混淆,因为微软的命名是anything but consistent。 为此,您需要使用 .NET Core 指南:Build, test, and deploy .NET Core apps

    为了节省您的时间,这里有一个用于构建 ASP.NET Core 应用程序的简单 YAML 管道:

    trigger:
      branches:
        include:
          - master
    
    # Setup pipeline-level variables to keep things DRY
    variables:
      configuration: Release
      projects: '**/*.csproj'
      publish_dir: $(Build.ArtifactStagingDirectory)
      vm_image: ubuntu-latest
    
    stages:
      - stage: Build
        jobs:
          - job: dotnet
            displayName: .NET
    
            pool:
              vmImage: $(vm_image)
    
            # Run builds in latest .NET 6 SDK container (Debian 11)
            # This simplifies things and allows to easily target different SDKs:
            # https://hub.docker.com/_/microsoft-dotnet-sdk
            container: mcr.microsoft.com/dotnet/sdk:6.0-bullseye-slim
    
            workspace:
              clean: all
    
            # Build tasks a separated (restore/build/test/publish)
            # to make it easier to catch/debug issues
            steps:
              - task: DotNetCoreCLI@2
                displayName: .NET | Restore [$(configuration)]
                inputs:
                  command: restore
                  projects: $(projects)
    
              - task: DotNetCoreCLI@2
                displayName: .NET | Build [$(configuration)]
                inputs:
                  command: build
                  projects: $(projects)
                  arguments: --configuration $(configuration) --no-restore
    
              - task: DotNetCoreCLI@2
                displayName: .NET | Test [$(configuration)]
                inputs:
                  command: test
                  projects: $(projects)
                  publishTestResults: true
                  arguments: --configuration $(configuration) --no-restore --no-build
    
              - task: DotNetCoreCLI@2
                displayName: .NET | Publish [$(configuration)]
                inputs:
                  command: publish
                  publishWebProjects: true
                  zipAfterPublish: true
                  modifyOutputPath: true
                  arguments: --configuration $(configuration) --output $(publish_dir) --no-restore --no-build
    
              # Publish zipped build results so they can be downloaded from the pipeline UI
              - publish: $(publish_dir)
                displayName: Artifact | Publish
                artifact: $(Build.DefinitionName)
    

    【讨论】:

      【解决方案2】:

      阅读这 2 个链接后,Azure devops 的模板存在 2 个问题。

      1. 恢复包一直在寻找 SDK 5。所以这个任务解决了这个问题。我必须包含一个提到 SDK 6 的步骤。https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/package/nuget?view=azure-devops
      2. 它还继续针对不支持 .NET 6.0.x 的 Visual Studio 2019。因此,而不是vmImage: 'windows-latest',使用vmImage: 'windows-2022' 允许构建成功https://github.com/dotnet/core/issues/6907

      我最终使用了经典模板,以便更好地了解管道的工作原理。 1) 使用 ubuntu-latest 或 windows-2022。 2) 为SDK的版本增加一个步骤。

      【讨论】:

        猜你喜欢
        • 2021-12-12
        • 2019-09-08
        • 2022-08-21
        • 2023-01-16
        • 1970-01-01
        • 1970-01-01
        • 2020-12-16
        • 2022-08-05
        相关资源
        最近更新 更多