.NET 5 cant be selected as Target Framework in VS2019, reference assemblies not found

16,631

Solution 1

Finally found a solution for this problem. As VS somehow is loosing track of what .Net version it should use I needed to "force" the program to use the intended version. This need to be done on a "per solution basis" and are done by adding a global.json file to the root of the solution that contains info about what exact version is needed.

This can be done by:

  1. in VS open View -> Terminal and enter: dotnet new globaljson
  2. Open the root folder of the solution and edit the newly created file "global.json" the exact name of the .net version installed must be entered, to find this out run the open CMD and run dotnet --version (or dotnet --info if you got multiply entry's and need to find a specific one)
  3. add the correct version to the global.json file, save the file and restart VS (the image below is what my global.json looks like)

enter image description here

Doing this solved my problem and VS can now correctly find my .Net 5 installation. This also allowed me to VS 2019 version 16.7.6 with .net 5.0 preview 7 (no VS preview needed). It is worth mentioning that the VS setting "Use previews of .net Core" mentioned in the original post still need to be activated (in non-preview versions of VS)

EDIT #1:

Here is where you find more info about the global.json: https://docs.microsoft.com/en-us/dotnet/core/tools/global-json?tabs=netcore3x

EDIT #2: .net 6

The same machine had the very same problem when going to .net 6, older versions where showing in vs22 but not .net 6. using a global.json to target .net6 did the trick here.

Solution 2

You must upgrade your VS version. .NET 5 is compatible with version 16.8 of Visual Studio. If you are using VSCode instead, use a global.json file to specify the dotnet version you want to use.

Solution 3

Actually, in my side, I did not face the same error.

enter image description here

enter image description here

enter image description here

So it is quite strange. And I am afraid that there are some errors on your NET5, VS or OS environment.

So please try these steps to troubleshoot it:

Suggestion

1) disable any third vs installed extensions under Extensions-->Manage Extensions-->Installed.

2) delete all component model caches under C:\Users\xxx(current user)\AppData\Local\Microsoft\VisualStudio\16.0_xxx\ComponentModelCache

3) reset all vs settings under Tools--->Import and Export Settings-->Reset all settings

4) open Control Panel-->Programs-->Programs and Features-->Uninstall a Program--> Right-click on Microsoft .NET SDK 5.0.100-preview.7.20366(x64)-->Change-->Repair

5) repair VS on the vs_installer

Share:
16,631

Related videos on Youtube

user10483669
Author by

user10483669

Updated on June 04, 2022

Comments

  • user10483669
    user10483669 almost 2 years

    I cant get Visual studio to use .NET 5.

    The following are my current configuration: Visual Studio Professional 2019 version: 16.7.6

    .NET sdk: dotnet-sdk-5.0.100-preview.7.20366.6-win-x64 (see image for more info)

    enter image description here

    Visual Studio settings: I have selected "Use previews of the .NET Core SDK" in VS (See image) enter image description here

    Possible source of my problem: I accidentally installed preview 8 of .NET 5, this version don't support my version of Visual Studio. I removed this version and installed the correct one (preview 7 should have support for VS 16.7, see: https://dotnet.microsoft.com/download/dotnet/5.0)

    These are the steps I take to reproduce my problem:

    in visual studio I do the following to create a .net 5 project (blazor project):

    1. start vs 2019
    2. select "Create a new project"
    3. select "Blazor App"
    4. give the project a name/Location/solution name and click "Create"
    5. select .Net 5.0 (.net core 3.1 is also available) and Blazor Server App and click "Create" (see image) enter image description here

    6.the project is now created but a error message is instantly displayed "the reference assemblies for .NETFramework, Version=v5.0 were not found. ..." (see image) enter image description here

    7.right click on the project -> Properties 8. "Target framework" are now blank, If i open the dropdown only .Net framework versions are available (see image) enter image description here

    Other information that might have value: If i creat a new project and select .Net core 3.1 instead of .net 5 everything is working fine. Doing this alows me to see items other than .Net framework in the "Target frameworks" dropdown, .NET 5 are however still missing. (see image)

    enter image description here

    If i go to "Add or remove programs" .Net 5 of the correct version are listed. (it is also the only listed version of .NET 5)

    If i look at the system path it is correctly set to: "C:\Program Files\dotnet"

    Why is this happening? how can i get .net 5 to correctly show in this list?

    #Update 1: After installing Visual studio 16.8.0 Preview 5.0 the problem still persists .NET 5 can't be used. If i look at Dependencies it is clear that something is wrong, non of the entries under Dependencies -> Assemblies are found/has a value (see image)

    enter image description here

    #Update 2: finally found a solution 4 this problem using global.json, see my own answer for more details

    • Pavel Anikhouski
      Pavel Anikhouski over 3 years
      Afaik, .NET 5 is available only in VS 16.8, so you should install the latest 16.8 preview version instead of 16.7
    • user10483669
      user10483669 over 3 years
      @PavelAnikhouski this is from the link i posted, regarding .net 5 prev 7 VS support: "Visual Studio support: Visual Studio 2019 (v16.7, latest preview)", as you can see v16.7 is listed here. Link: dotnet.microsoft.com/download/dotnet/5.0
    • Panagiotis Kanavos
      Panagiotis Kanavos over 3 years
      @user10483669 no it's not. Visual Studio support: Visual Studio 2019 (v16.8, Preview 4) The latest .NET 5 version, RC2, is supported in the latest VS 16.8 preview. Upgrade your VS version. Only the RC versions have a Go-Live license and support in production. You're using unsupported versions. Upgrade to the supported versions
    • Panagiotis Kanavos
      Panagiotis Kanavos over 3 years
      @user10483669 besides, in 1 week you'll have to upgrade anyway when .NET 5 is released. Why fight incompatibilities that are already solved?
    • user10483669
      user10483669 over 3 years
      @PanagiotisKanavos why is v16.7 non preview listed then? maybe supported where the wrong words to use, compatible would probably have been better as these 2 versions indeed are compatible.
    • Panagiotis Kanavos
      Panagiotis Kanavos over 3 years
      It doesn't matter since they aren't supported. They won't get any fixes - the fixes (and breaking changes) are already in the RC versions. The code you write there will probably break next week when the release comes out
    • Panagiotis Kanavos
      Panagiotis Kanavos over 3 years
      Why are you trying to use versions that you already know aren't compatible with the 5.0 release?
  • user10483669
    user10483669 over 3 years
    followed these instructions without any success, the problem still persists. any other ideas?
  • Mr Qian
    Mr Qian over 3 years
    Maybe you could try to delete the folder C:\Users\xxx(current user)\AppData\Local\Microsoft\VisualStudio\16.0_xxx
  • user10483669
    user10483669 over 3 years
    I installed VS prev and still get the same error, it are as if VS think that the TFM 'net5.0' is .net framework 5 instead of .net core 5
  • Panagiotis Kanavos
    Panagiotis Kanavos over 3 years
    VS isn't losing track of the versions, you are using incompatible and unsupported versions. The only .NET 5 versions with a Go-Live license, 5.0 RC1 and RC2, require the latest VS 16.8 previews
  • Panagiotis Kanavos
    Panagiotis Kanavos over 3 years
    No, I'm not wrong. Nor is it something hidden in the download page, this has been announced over 6 months ago, explained in blog posts, announcements, conferences, over and over again. The very page you listed says the supported verions are the RCs. Again, why do you insist on using unsupported versions that will have to be discarded next week?
  • user10483669
    user10483669 over 3 years
    @PanagiotisKanavos I'm running this setup on 3 different machines, only the one where I accidentally installed the wrong version first have had any problems, the other two have been working "out of the box"
  • Panagiotis Kanavos
    Panagiotis Kanavos over 3 years
    So you say the .NET Core team don't know what they're doing or even supporting?
  • user10483669
    user10483669 over 3 years
    @PanagiotisKanavos then plz give me a link to where its stated that these versions are incompatible (VS v16.7 and SDK 5.0.100-preview.7)