Azure Devops - The current .NET SDK does not support targeting .NET 6.0

Created on

The solution to solving Azure Devops Error NETSDK1045 when using .NET 6.0 in a pipeline.


Azure DevOps Pipeline Issue NET 6.0

I recently updated my ASP.NET Core web application to .NET 6.0.

This has caused my Azure DevOps pipeline to fail with the following error:

##[error]The nuget command failed with exit code(1) and error(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. [D:\a\1\s\WebApp1\WebApp1.csproj]

This is because, as of 11/27/2021, Azure DevOps does not automatically include the .NET 6.0 SDK in the pipeline.

Additionally, as of 11/27/2021, Azure Devops vmImage value of windows-latest does not default to the latest windows-2022 that has Visual Studio 2022 available.

Solution to Fix Azure DevOps .NET 6.0 SDK Issue

We need to update the pipeline to include the .NET 6.0 SDK.

We will add 3 steps to our pipeline to make it work with our .NET 6.0 project:

  1. Update the vmImage to be windows-2022
  2. Add a command to include the .NET 6.0 SDK
  3. Add a command to list the current .NET SDKs used in the pipeline (sanity check)

This is a fully working Azure DevOps pipeline for a .NET 6.0 project using C# 10.0:

# ASP.NET Core (.NET Framework)
# Build and test ASP.NET Core projects targeting the full .NET Framework.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core

trigger:
- master

pool:
  vmImage: 'windows-2022'

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

steps:

# Add this Command to Include the .NET 6 SDK
- task: UseDotNet@2
  displayName: Use .NET 6.0
  inputs:
    packageType: 'sdk'
    version: '6.0.x'

# Add a Command To List the Current .NET SDKs (Sanity Check)
- task: CmdLine@2
  inputs:
    script: 'dotnet --list-sdks'

- 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:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

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

Your pipeline should now run successfully!

Azure DevOps - The Feature 'Global Using Directive' is currently in Preview

Additional References

Microsoft Reference for Error: NETSDK1045