Search code examples
.netvisual-studio.net-coreinstallation.net-5

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


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


Solution

  • 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://learn.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.