53

I know this is a duplicate question. But I have tried every mentioned solution and didn't get resolved.

I have a dotnet c# application. when I run the application I got the below-mentioned error

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.

All the following solutions I have tried

  1. Download and install the dotnet-SDK-6.0.300 version.
  2. Updated the VS-2019 and checked the " Use preview of the .NET SDK (required restart)

Windows Edition: Windows 11

Still, it's not working. Got the same error. Help me out of this.

jithu
  • 1,867
  • 1
  • 7
  • 16
  • 1
    I have this issue as well, did the same things, got the same results. In my case, I'm trying to get a clone of ImGui.NET to build. – Bryan Edds May 28 '22 at 17:49
  • @BryanEdds Please check the verified answer of this question for the solution – jithu May 30 '22 at 05:43
  • Generally happens when global.json has lesser version than the version targetted in csproj. – J Santosh Jan 14 '23 at 13:11
  • Related post - [The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a .NET SDK that supports .NET Core 2.1](https://stackoverflow.com/q/49171623/465053) – RBT May 04 '23 at 05:05

10 Answers10

99

.NET 6 is supported with Visual Studio 2022 and Visual Studio 2022 for Mac. It is not supported with Visual Studio 2019, Visual Studio for Mac 8, or MSBuild 16. If you want to use .NET 6, you will need to upgrade to Visual Studio 2022 (which is also now 64-bit).

jithu
  • 1,867
  • 1
  • 7
  • 16
  • 20
    I am using Visual Studio 2022 for Mac 17.0.2 and am still experiencing the issue. This is a large project that we just upgraded from 5 to 6. My target framework: net6.0 My global.json { "sdk": { "version": "6.0.300" } } – whendon Jun 09 '22 at 05:13
  • 9
    To add some clarity as to how I experienced this. I have both VS2022 and VS2019 installed on my machine and I created the project in 2022, but when I double clicked to open the sln file, it opened 2019. I hadn't realized it did that, so I experienced the error message in this question. – John Koerner Aug 31 '22 at 01:43
  • I am using VS 2019 on windows 10, this answer doesn't apply to windows users – Altaf Sep 20 '22 at 15:53
  • I have VS 2019 and VS 2022, build is successful on VS 2022 and failing on VS 2019. I am windows 11 user. Installing VS 2022 solved my problem. Thanks for the answer @jithu – Dodda Venkata Oct 12 '22 at 14:44
  • There were some indications that allowing pre-release in VS2019 may server to override the 2022 requirement. I haven't been able to verify. See response from Alexey L: https://dotnet.microsoft.com/en-us/download/dotnet/6.0 https://learn.microsoft.com/en-us/answers/questions/620284/how-to-target-net-6-0-with-visual-studio-2019 – Jamie Jan 11 '23 at 15:45
  • I had the same issue with VisualStudio 2022 and, the .net 8 preview, and updating VS to the latest version fixed the issue. – Mohammad Taherian Aug 09 '23 at 14:32
9

I experienced this issue while upgrading a project from .NET 5 to .NET 6, using Visual Studio 2022.

In my case, the problem was the existence of a global.json file fixing the SDK version:

{
  "sdk": {
    "version": "5.0.0",
    "rollForward": "latestMinor"
  }
}

The error was gone simply by updating that file to 6.0.0.

Tao Gómez Gil
  • 2,228
  • 20
  • 36
  • This got me too when trying to move from 5 to 6 as well. I'm not sure where this file came from, as it wasn't checked in to source control. Removing the file worked to allowed me to build against 5, 6, and 7. So far the file hasn't recreated itself. – nitz Jan 18 '23 at 13:15
  • Had given up. IDE was not showing .NET 6.0 in the Target framework field. I had even reinstalled the framework. Many thanks. – Joseph Waweru Apr 25 '23 at 21:53
  • If you are still getting errors, do a dotnet tool restore after changing the Visual Studio Terminal window. Worked for me. – Ashley Aug 30 '23 at 21:44
6

.NET 6 is supported with Visual Studio 2022 and Visual Studio 2022 for Mac. It is not supported with Visual Studio 2019. If you want to use .NET 6, you will need to upgrade to Visual Studio 2022.

Below is the official announcement by Microsoft community. enter image description here

Bhadresh Patel
  • 1,671
  • 17
  • 18
5

If you are trying to set up docker, open your ".csproj" file and change the Target Framework version to be the same as the one in your Dockerfile

<PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
    <DockerfileContext>..\..\..</DockerfileContext>
</PropertyGroup>
Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
CutCopyPaste
  • 51
  • 1
  • 2
2

On AWS CodeBuild if you want to run (not yet supported on AWS) .NET 7.0, use this buildspec. The channel needs to be STS

phases:
  install:
    commands:
      - /usr/local/bin/dotnet-install.sh --channel STS

  build:
    commands:
      - dotnet restore
      - dotnet build --configuration Release BackendApi.csproj
      - dotnet publish --configuration Release BackendApi.csproj --output ./publish
realPro
  • 1,713
  • 3
  • 22
  • 34
1

I found an interesting reason to workaround. I have checked all the above instructions, using VS 2022, installed .Net 6, taking updates but still, issues exist then I check my Dockerfile and found an interesting reason, there was pointing wrong .net version but I set .net version 5 to 6 through project property. I just pointed to .net 6 in the docker file as a base and the build now working fine.

Sample base and build .net 6 command

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
and for build
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
Mahi
  • 1,019
  • 9
  • 19
  • I had same issue. I copied file from my old project and in that I was using .net core 5.0 and in newer project I was using 7.0. so I have to update the version and it started working for me. – Mukesh Salaria Jun 12 '23 at 12:34
0

I experienced the same issue while building the docker image after upgrading from .NET 5 to 6. I solved it by creating a new docker image and building it again.

0

Got this error just after upgrading from vs2019 to vs2022. Just restarted my workstation and it was gone.

Nim.th
  • 11
  • 2
  • Also, I was getting this error, and realize I opened the VS 2019 instead of 2022 - user error – TonyE May 17 '23 at 18:56
0

For me, I accidentally had the "Build with MSBuild on Mono" checkbox checked.

This is located in Solution Properties > Build > General

Ben
  • 1,853
  • 19
  • 20
0

Just to add another reason why this might be happening. In our case, definitely having the correct SDK installed, definitely targeting it correctly, and even having a global.json file with the correct sdk version in it did not work. The global.json file was definitely found, but ignored. This was on a build server.

Turns out, someone had defined an environment variable

MSBuildSDKsPath=C:\Program Files\dotnet\sdk\5.0.402\Sdks

... and dotnet stubbornly wanted to use that SDK. I have not found that documented anywhere. Unsetting that environment variable did the trick.

J S
  • 901
  • 8
  • 15