在 VS2019 中无法选择 .NET 5 作为目标框架,未找到引用程序集 [英] .NET 5 cant be selected as Target Framework in VS2019, reference assemblies not found

查看:334
本文介绍了在 VS2019 中无法选择 .NET 5 作为目标框架,未找到引用程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 Visual Studio 使用 .NET 5.

以下是我目前的配置:Visual Studio Professional 2019 版本:16.7.6

.NET sdk:dotnet-sdk-5.0.100-preview.7.20366.6-win-x64 (查看图片了解更多信息)

Visual Studio 设置:我选择了使用 .NET Core SDK 的预览"在 VS (见图片)

我的问题的可能来源:我不小心安装了 .NET 5 的预览版 8,这个版本不支持我的 Visual Studio 版本.我删除了这个版本并安装了正确的(预览版 7 应该支持 VS 16.7,请参阅:

6.项目现已创建,但立即显示错误消息未找到 .NETFramework 的参考程序集,版本 = v5.0...."(见图)

7.右键单击项目->特性8.目标框架"现在是空白的,如果我打开下拉菜单,只有 .Net 框架版本可用(见图)

其他可能有价值的信息:如果我创建一个新项目并选择 .Net core 3.1 而不是 .net 5 一切正常.这样做可以让我在目标框架"中看到除 .Net 框架之外的项目.下拉菜单中,.NET 5 仍然缺失.(见图)

如果我转到添加或删除程序"列出了正确版本的 .Net 5.(它也是唯一列出的 .NET 5 版本)

如果我查看系统路径,它被正确设置为:C:Program Filesdotnet"

为什么会这样?如何让 .net 5 正确显示在此列表中?

#更新 1:安装 Visual Studio 16.8.0 Preview 5.0 后,问题仍然存在,无法使用 .NET 5.如果我查看 Dependencies 很明显有问题,Dependencies 下的条目都不是 ->找到程序集/具有值(见图)

#更新 2:最终使用 global.json 找到了解决此问题的解决方案 4,有关详细信息,请参阅我自己的答案

解决方案

终于找到了这个问题的解决方案.由于 VS 不知何故失去了它应该使用的 .Net 版本的轨道,我需要强制"使用预期版本的程序.这需要在每个解决方案的基础上" 完成,并通过将 global.json 文件添加到解决方案的根目录来完成,该文件包含有关确切版本的信息需要.

这可以通过:

  1. 在VS中打开视图->终端并输入:dotnet new globaljson
  2. 打开解决方案的根文件夹并编辑新创建的文件global.json";必须输入安装的 .net 版本的确切名称,要找到它,请运行打开的 CMD 并运行 dotnet --version (或 dotnet --info如果您有多个条目并且需要找到一个特定的条目)
  3. 将正确的版本添加到 global.json 文件中,保存文件并重新启动 VS (下图是我的 global.json 的样子)

这样做解决了我的问题,VS 现在可以正确找到我的 .Net 5 安装.这也让我能够使用 .net 5.0 预览版 7(不需要 VS 预览版)VS 2019 版本 16.7.6.值得一提的是,原帖中提到的VS设置Use previews of .net Core"仍然需要激活(在VS的非预览版本中)

编辑 #1:

您可以在此处找到有关 global.json 的更多信息:https://docs.microsoft.com/en-us/dotnet/core/tools/global-json?tabs=netcore3x

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)

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

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)

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)

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)

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)

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 Filesdotnet"

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)

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

解决方案

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)

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

这篇关于在 VS2019 中无法选择 .NET 5 作为目标框架,未找到引用程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆