Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 449
Iucounu Avatar asked Jan 18 '18 14:01

Iucounu


People also ask

Is .NET framework compatible with .NET standard?

Broad platform support. . NET Standard 2.0 is supported on the following platforms: . NET Framework 4.6.

Can I use a .NET framework library with .NET 6?

Yes, you can load . NET Framework assemblies into . NET Core 5 and 6.

How do I add a net standard library?

Right-click on the solution in Solution Explorer and select Add > New Project. On the Add a new project page, enter library in the search box. Choose C# or Visual Basic from the Language list, and then choose All platforms from the Platform list. Choose the Class Library template, and then choose Next.

What is .NET standard library?

. Net Standard is a specification which dictates what the Base Class Libraries of different . Net platforms should implement to unify the Base Class Libraries of different . Net Platforms. Here, Platform means full .


1 Answers

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.

like image 129
NightOwl888 Avatar answered Oct 09 '22 16:10

NightOwl888