NETSDK1045: The Current .NET SDK Does Not Support Targeting .NET 6.0

Recently, I was working with an asp.net 6 project and found an issue with Azure DevOps. I had set up a build pipeline in Azure DevOps for the repository. However, my build pipeline was failing for every commit and push. Then I started checking error in the Azure CI Build pipelines and found the below error.

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.

Exact 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\ProjectName.csproj]

This error was same for every .NET 6 projects.

After some research, I found that .NET is not installed in Azure Host agent. 

The reason for this error is the .NET 6 project. Azure Pipelines uses .NET 5 as latest version in its hosted build agents. It means the host machine has .NET 5 version as latest and .NET 6 is missing.

Since .NET6 is not installed yet in Microsoft hosted build agents in Azure DevOps. i.e., .NET 6 is still not supported in Azure DevOps pipelines.

Solution

To resolve the above error or issues, we can add a task before build task. We need to specify the .NET core SDK version before build task as shown below:

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

Complete azure-pipelines.yml file sample.

# ASP.NET Core
# Build and test ASP.NET Core projects targeting .NET Core.
# Add steps that run tests, create a NuGet package, deploy, and more:
#  https://docs.microsoft.com/azure/devops/pipelines/languages/dotnet-core 

trigger:
- master

pool:
  vmImage: windows-latest

variables:
  buildConfiguration: 'Release'

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

- script: dotnet build --configuration $(buildConfiguration)
  displayName: 'dotnet build $(buildConfiguration)'

This is how I was able to resolve this Azure DevOps error.

Kind Regards