Visual Studio 2017 won't load .NET Framework references in .NET Standard library

回眸只為那壹抹淺笑 提交于 2020-01-11 04:46:27

问题


I've installed Visual Studio 2017. I have a class library in the new .NET Standard format (which is able to surface both .NET Framework and .NET Core). But when I go to "Add" - "Reference", then "Assemblies" - "Framework", it spins for a long time and then says, "No Framework assemblies were found on the machine." (This machine has VS 2015 installed and running fine, as well as .NET 4.6.1.) How do I resolve this?

ETA: 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 from

<TargetFramework>netstandard2.0</TargetFramework>

to

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

allows me to finally add the reference to System.Runtime.Caching, but it's got a yellow warning icon in the IDE when expanding the references, and is included under both .NET 4.6.1 and Standard in the collapsible sections (where Standard also shows the warning icon). Builds fail because the IDE claims that the reference is still missing.


回答1:


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




回答2:


On my side, I've tried all the solution presented before but the solution was simply install NuGet package for Microsoft.CSharp.

After installation just clean the project and restart your IDE.




回答3:


Try to change order of TargetFrameworks inside your .csproj.

From

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

To

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



回答4:


This happened to me when I opened a solution targeting 4.7.1 on a fresh-install PC where only 4.7.2 was present



来源:https://stackoverflow.com/questions/48324121/visual-studio-2017-wont-load-net-framework-references-in-net-standard-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!