简体   繁体   English

Visual Studio 2017 不会在 .NET Standard 库中加载 .NET Framework 引用

[英]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.

相关问题 如何从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? 在Visual Studio 2017中选择.NET Target Framework - Selecting .NET Target Framework in Visual Studio 2017 Visual Studio 2017 - 从 .Net Framework 4.52 升级到 4.61 后无法更新服务引用 - Visual Studio 2017 - After upgrade from .Net Framework 4.52 to 4.61 unable to update service references Visual Studio 2017使用.net标准库时复制太多文件 - Visual Studio 2017 Copy too many files when using a .net standard library 如何在Visual Studio 2017中将PCL更改为.net平台标准库? - How can I change a PCL into a .net Platform Standard Library in Visual Studio 2017? 如何在 Visual Studio 2019 中调试 .NET 5 WinForms 应用程序,该应用程序引用 .NET Framework 4.8 class 库 - Howto debug .NET 5 WinForms application that references .NET Framework 4.8 class library in Visual Studio 2019 Visual Studio .NET标准库和.NET Framework项目-NuGet dll清单定义不匹配 - Visual Studio .NET Standard library and .NET Framework project - NuGet dll manifest definition does not match 如何在 Visual Studio 中将 .NET Framework 更改为 .NET Standard/Core? - How to change .NET Framework to .NET Standard/Core in Visual Studio? Class 库(.NET 标准)模板在 Visual Studio 2019 中不起作用 - Class Library (.NET Standard) Template Not working in Visual Studio 2019 Visual Studio .net 标准库模板使用指令中断? - Visual Studio .net standard library template breaks using directive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM