你如何在 VS2017 中引用 .net 项目中的 netstandard 项目?

How do you reference a netstandard project from .net project in VS2017?

我有一个 netstandard2.0 项目,用于与第 3 方 Web 服务交互。我需要在同一解决方案中从较旧的 .net 4.6.2 项目引用该项目,但是当我这样做时,我会收到一堆关于需要引用我定义的类型的错误。

例如,我将在 netstandard 库中调用 returns 任务的方法,但我会收到一条错误消息,提示我需要对 System.Threading.Tasks 的引用,即使我已经有了一个.

我认为这里有两个问题。

只有特定版本的 .netstandard 可以与特定版本的 .net 一起使用 4.X.Y

要解决此问题,您可能需要更改一个或两个项目的版本。使用图表找出哪些版本兼容:https://docs.microsoft.com/en-us/dotnet/standard/net-standard

  1. Select 你的项目
  2. 右键单击属性
  3. 在 "Application" 选项卡上使用 "Target Framework" 下拉菜单根据需要更改框架

.net 4.X.Y 使用 4.0.0.0 系统库,而 .netstandardX.Y 使用 4.3.0.0

要解决此问题,您需要添加并引用 netstandard.library NuGet 包。

  1. Select 你的项目
  2. 右击"Manage NuGet Packages..."
  3. 在 "Browse" 选项卡上搜索 "netstandard.library"
  4. Select你需要的版本并安装 (我相信这取决于您最终使用的 .netstandard 版本)
  5. 导航到每个引用错误,右键单击它,然后添加缺少的任何引用