[英]Visual Studio 2017 won't load .NET Framework references in .NET Standard library
I've installed Visual Studio 2017. I have a class library in the new .NET Standard format, which is able to be used by both .NET Framework and .NET Core.我已经安装了 Visual Studio 2017。我有一个新的 .NET Standard 格式的类库,.NET Framework 和 .NET Core 都可以使用它。 But when I go to Add… Reference… Assemblies Framework , Visual Studio spins for a long time and then says,
但是当我去Add... Reference... Assemblies Framework时,Visual Studio 旋转了很长时间然后说,
No Framework assemblies were found on the machine.
在机器上找不到任何框架程序集。
(This machine also has Visual Studio 2015 installed, as well as .NET 4.6.1.) (这台机器还安装了 Visual Studio 2015,以及 .NET 4.6.1。)
How do I resolve this?我该如何解决这个问题?
My .csproj
file currently looks like this:我的
.csproj
文件目前如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Utility\EncryptionUtility.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Utility\" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
</Project>
Changing the target framework from:从以下位置更改目标框架:
<TargetFramework>netstandard2.0</TargetFramework>
to至
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
Allows me to finally add a reference to System.Runtime.Caching
, but it has a yellow warning icon in the IDE when expanding the references.允许我最终添加对
System.Runtime.Caching
的引用,但在扩展引用时它在 IDE 中有一个黄色警告图标。 It is included under both .NET 4.6.1 and .NET Standard in the collapsible sections, with the reference under Standard also shows the warning icon.它包含在 .NET 4.6.1 和 .NET Standard 的可折叠部分中,Standard 下的参考也显示了警告图标。 Builds fail because the IDE claims that the reference is still missing.
构建失败,因为 IDE 声称该引用仍然丢失。
When multi-targeting both .NET Framework and .NET Core/.NET Standard you will almost certainly need to use MSBuild Conditions to prevent .NET Framework references from bleeding over into .NET Core/.NET Standard.当同时针对 .NET Framework 和 .NET Core/.NET Standard 进行多目标处理时,您几乎肯定需要使用MSBuild 条件来防止 .NET Framework 引用流入 .NET Core/.NET Standard。
MSBuild conditions have been around for quite some time, but there is no support in Visual Studio to add them, you have to manually edit your .csproj
file. MSBuild 条件已经存在了很长一段时间,但 Visual Studio 不支持添加它们,您必须手动编辑
.csproj
文件。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Utility\EncryptionUtility.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Utility\" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System.Runtime.Caching" />
</ItemGroup>
</Project>
Also note that once you do this, there are no guarantees it will work right to add a NuGet or other assembly reference using Visual Studio - you may need to do manual cleanup every time in the
.csproj
file to ensure the reference is added to the right conditional section.另请注意,一旦执行此操作,无法保证使用 Visual Studio 添加 NuGet 或其他程序集引用会正常工作 - 您可能需要每次在
.csproj
文件中进行手动清理,以确保将引用添加到右条件部分。 You are probably better off adding references by hand-editing the file every time.通过每次手动编辑文件来添加引用可能会更好。
On my side, I've tried all the solution presented before but the solution was simply install NuGet package for Microsoft.CSharp.就我而言,我已经尝试过之前提出的所有解决方案,但解决方案只是为 Microsoft.CSharp 安装 NuGet 包。
After installation just clean the project and restart your IDE.安装后只需清理项目并重新启动 IDE。
Try to change order of TargetFrameworks inside your .csproj.尝试更改 .csproj 中 TargetFrameworks 的顺序。
From从
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
To至
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
当我在仅存在 4.7.2 的全新安装 PC 上打开针对 4.7.1 的解决方案时,这发生在我身上
As an alternative, you can use the .NET Standard Library from Nuget Package Manager to handle this issue:作为替代方案,您可以使用Nuget 包管理器中的 .NET 标准库来处理此问题:
The message in the Add Reference window for .NET Framework is expected. .NET Framework 的“添加引用”窗口中的消息是预期的。 When you create a .NET Standard library, the
NETStandard.Library
metapackage is automatically referenced during project creation.创建 .NET Standard 库时,会在项目创建期间自动引用
NETStandard.Library
元包。 It is a set of standard .NET APIs that are recommended to be used and supported together.它是一组建议一起使用和支持的标准 .NET API。 This includes all of the APIs in the
NETStandard.Platform
package, plus additional libraries that are core to .NET, but built on top of NETStandard.Platform
.这包括
NETStandard.Platform
包中的所有 API,以及 .NET 核心但构建在NETStandard.Platform
之上的其他库。
This means we don't need to need add references individually.这意味着我们不需要单独添加引用。
只需为相应的框架安装 .NET Framework 开发人员包,它就可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.