Visual Studio 2017 不会在 .NET Standard 库中加载 .NET Framework 引用 [英] Visual Studio 2017 won't load .NET Framework references in .NET Standard library

查看:24
本文介绍了Visual Studio 2017 不会在 .NET Standard 库中加载 .NET Framework 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了 Visual Studio 2017.我有一个新的 .NET Standard 格式的类库,它可以被 .NET Framework 和 .NET Core 使用.但是当我去Add... Reference... Assemblies Framework 时,Visual Studio 旋转了很长时间然后说,

<块引用>

在机器上没有找到框架程序集.

(本机还安装了Visual Studio 2015,以及.NET 4.6.1.)

我该如何解决这个问题?

我的 .csproj 文件目前看起来像这样:

<属性组><TargetFrameworks>net461;netstandard2.0</TargetFrameworks></PropertyGroup><项目组><Compile Remove="UtilityEncryptionUtility.cs"/></项目组><项目组><文件夹包含="实用程序"/></项目组><项目组><Reference Include="System.Runtime.Caching"/></项目组></项目>

改变目标框架:

netstandard2.0

net461;netstandard2.0

允许我最终添加对 System.Runtime.Caching 的引用,但是当展开引用时,它在 IDE 中有一个黄色警告图标.它包含在 .NET 4.6.1 和 .NET Standard 的可折叠部分中,Standard 下的参考也显示警告图标.构建失败,因为 IDE 声称引用仍然丢失.

解决方案

当同时面向 .NET Framework 和 .NET Core/.NET Standard 时,您几乎肯定需要使用 MSBuild 条件 以防止 .NET Framework 引用渗入 .NET Core/.NET Standard.>

MSBuild 条件已经存在一段时间了,但 Visual Studio 不支持添加它们,您必须手动编辑您的 .csproj 文件.

<属性组><TargetFrameworks>net461;netstandard2.0</TargetFrameworks></PropertyGroup><项目组><Compile Remove="UtilityEncryptionUtility.cs";/></项目组><项目组><文件夹包含=实用程序"/></项目组><项目组条件="'$(TargetFramework)' == 'net461' "><Reference Include=System.Runtime.Caching"/></项目组></项目>

<块引用>

另请注意,一旦您执行此操作,无法保证使用 Visual Studio 添加 NuGet 或其他程序集引用会正常工作 - 您可能需要每次在 .csproj 文件以确保将引用添加到正确的条件部分.您最好每次都手动编辑文件来添加参考文献.

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.

(This machine also has Visual Studio 2015 installed, as well as .NET 4.6.1.)

How do I resolve this?

My .csproj file currently looks like this:

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

  <PropertyGroup>
    <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <Compile Remove="UtilityEncryptionUtility.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. 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.

解决方案

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 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="UtilityEncryptionUtility.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. You are probably better off adding references by hand-editing the file every time.

这篇关于Visual Studio 2017 不会在 .NET Standard 库中加载 .NET Framework 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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