如何在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?

查看:623
本文介绍了如何在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="..\Common\Common.csproj" />
  </ItemGroup>

</Project>

构建控制台应用程序时,出现以下构建错误:

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

C:\Program Files\dotnet\sdk\1.0.4\NuGet.targets(97,5):错误:Project Common与net45(.NETFramework,Version = v4.5)。 Project Common支持:netstandard1.6(.NETStandard,Version = v1.6)

我看到了此问题并尝试了提供的一些建议在那里,但他们都没有工作。因此,这似乎是一个不同的问题。请注意,这是在我的解决方案构建期间发生的,并且不会以任何方式引用(显式)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天全站免登陆