如何从 Visual Studio 2017 中的 .NET Framework 4.5 控制台应用程序引用 .NET 标准库? [英] How Do You Reference a .NET Standard Library from a .NET Framework 4.5 Console Application in Visual Studio 2017?

查看:28
本文介绍了如何从 Visual Studio 2017 中的 .NET Framework 4.5 控制台应用程序引用 .NET 标准库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于安装了 Visual Studio 2017.2 并且正在尝试让我的第一个项目工作,但遇到了一些我希望在这里解决的问题.

I have finally installed Visual Studio 2017.2 and am trying to get my first project working, but am running into some trouble that I hope to address here.

我有一个非常简单的 .NET 标准库,描述为以下项目文件:

I have a very simple .NET Standard Library described as the following project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>

</Project>

还有一个非常简单的 .NET Framework 控制台应用程序,它引用了上述 .NET Standard 库,并被描述为以下项目文件:

And a very simple .NET Framework console application that references the above .NET Standard library, and is described as the following project file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net45</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..CommonCommon.csproj" />
  </ItemGroup>

</Project>

当我构建控制台应用程序时,我收到以下构建错误:

When I build my console application, I get the following build error:

C:Program Filesdotnetsdk1.0.4NuGet.targets(97,5): 错误:Project Common 与 net45 不兼容 (.NETFramework,Version=v4.5).项目通用支持:netstandard1.6 (.NETStandard,Version=v1.6)

我看到了 this question 并尝试了那里提供的一些建议,但没有一个奏效.所以,这似乎是一个不同的问题.请注意,这发生在我的解决方案的构建过程中,并且不会以任何方式引用(显式)NuGet 包.

I saw this question and tried some of the suggestions provided there, but none of them worked. So, this appears to be a different problem. Please note that this occurs during the build of my solution and not referencing (explicit) NuGet packages in any way.

最后,如果有帮助,我有一个解决方案可以在此处演示此问题:https://github.com/Mike-EEE/Stash/blob/master/VS2017.Multi/VS2017.dotNetFramework.sln

Finally, if it helps, I have a solution that demonstrates this issue here: https://github.com/Mike-EEE/Stash/blob/master/VS2017.Multi/VS2017.dotNetFramework.sln

推荐答案

.NET Framework 4.5 仅支持使用面向 .NET Standard 1.0 或 1.1 的 .net 标准库.由于您的库面向 1.6,因此该工具在此处执行正确的操作并出现错误(因为您的库可能使用 .NET Framework 4.5 中不可用的 API).如果您将库发布为 NuGet 包并通过包引用使用它,包还原也会出错(错误表明包不兼容).

.NET Framework 4.5 only supports using .net standard libraries targeting .NET Standard 1.0 or 1.1. Since your library targets 1.6, the tooling does the right thing here and errors out (since your library may use APIs not available in .NET Framework 4.5). If you published the library as NuGet package and consumed it via a package reference, the package restore would error out as well (with an error saying that the package is incompatible).

对于 .NET Framework 版本支持哪个 .NET Standard 版本存在一些混淆,尤其是因为有可用的预览工具(2.0")来更改这些版本.".NET 平台支持"表中的文档因此包含两行关于支持的版本.但是,在您的情况下,两个版本都将 .NET Framework 4.5 限制为 .NET Standard 1.1.

There is some confusion about which .NET Standard version a .NET Framework version supports especially since there is preview tooling available ("2.0") that changes these versions. The ".NET platforms support" table in the documentation therefore contains two lines about the supported versions. In your case however, both versions limit .NET Framework 4.5 to .NET Standard 1.1.

这篇关于如何从 Visual Studio 2017 中的 .NET Framework 4.5 控制台应用程序引用 .NET 标准库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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