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

6

10

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="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. 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.

Psycho answered 18/1, 2018 at 14:53 Comment(0)
L
7

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.

Labarum answered 2/2, 2018 at 8:35 Comment(0)
O
7

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.

Otte answered 16/9, 2018 at 21:11 Comment(0)
C
3

Try to change order of TargetFrameworks inside your .csproj.

From

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

To

<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
Cryan answered 18/1, 2018 at 14:58 Comment(4)
That was it! Thank you! It actually had just <TargetFramework> (not plural) to start with.Psycho
Yes, singular form may be used when project targets only one framework, glad I could help :)Cryan
Now there's a new issue... when I add a reference to System.Runtime.Caching, it adds it under both frameworks (seen by expanding the collapsible view in the IDE). The one added under Standard has a yellow warning icon, and there are associated build failures though the reference looks fine under .NET Framework 4.6.1. Any idea?Psycho
Use "Condition": <PackageReference Include="System.Runtime.Caching" Version="4.5.0-preview1-25914-04" Condition="'$(TargetFramework)' == 'net461'" />Kraigkrait
B
1

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

Berti answered 4/2, 2019 at 17:24 Comment(0)
U
0

As an alternative, you can use the .NET Standard Library from Nuget Package Manager to handle this issue:

Screenshot

The message in the Add Reference window for .NET Framework is expected. When you create a .NET Standard library, the NETStandard.Library metapackage is automatically referenced during project creation. It is a set of standard .NET APIs that are recommended to be used and supported together. 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.

This means we don’t need to need add references individually.

Unbound answered 6/3, 2020 at 5:23 Comment(0)
L
0

Just Install the .NET Framework developer pack forrespective framework and it works fine.

Lareine answered 17/8, 2021 at 6:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.