Visual Studio 2017不会在.NET Standard库中加载.NET Framework参考 [英] Visual Studio 2017 won't load .NET Framework references in .NET Standard library
问题描述
我已经安装了Visual Studio2017。我有一个新的.NET Standard格式的类库,.NET Framework和.NET Core都可以使用。但是当我转到 Add… Reference… 程序集 框架时,Visual Studio旋转了很长时间,然后说
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. But when I go to Add… Reference… Assemblies Framework, Visual Studio spins for a long time and then says,
在计算机上未找到框架程序集。
No Framework assemblies were found on the machine.
(此计算机还安装了Visual Studio 2015,以及.NET 4.6.1。)
(This machine also has Visual Studio 2015 installed, as well as .NET 4.6.1.)
我该如何解决?
我的 .csproj
文件当前如下所示:
My .csproj
file currently looks like this:
<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>
至
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
允许我最终添加对 System.Runtime.Caching的引用
,但在扩展引用时在IDE中具有黄色警告图标。它的可折叠部分包括在.NET 4.6.1和.NET Standard中,并且在Standard下的引用也显示警告图标。生成失败是因为IDE声称仍缺少该引用。
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. 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. Builds fail because the IDE claims that the reference is still missing.
推荐答案
同时面向.NET Framework和.NET Core的目标/.NET Standard,您几乎肯定会需要使用 MSBuild条件防止.NET Framework引用渗入.NET Core / .NET Standard。
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.
MSBuild条件已经存在了很长一段时间,但是在Visual Studio要添加它们,必须手动编辑 .csproj
文件。
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.
<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>
也请注意,一旦执行此操作,就无法保证会可以正常工作以使用Visual Studio添加NuGet或其他程序集引用-您可能每次需要在
.csproj
文件中进行手动清理,以确保将引用添加到右侧有条件的部分。您最好每次手动编辑文件来添加引用。
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. You are probably better off adding references by hand-editing the file every time.
这篇关于Visual Studio 2017不会在.NET Standard库中加载.NET Framework参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!