35

I have finally installed Visual Studio 2017.2 and am trying to get my first project working, but am running into some trouble that I hope to address here.

I have a very simple .NET Standard Library described as the following project file:

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

  <PropertyGroup>
    <TargetFramework>netstandard1.6</TargetFramework>
  </PropertyGroup>

</Project>

And a very simple .NET Framework console application that references the above .NET Standard library, and is described as the following project file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net45</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Common\Common.csproj" />
  </ItemGroup>

</Project>

When I build my console application, I get the following build error:

C:\Program Files\dotnet\sdk\1.0.4\NuGet.targets(97,5): error : Project Common is not compatible with net45 (.NETFramework,Version=v4.5). Project Common supports: netstandard1.6 (.NETStandard,Version=v1.6)

I saw this question and tried some of the suggestions provided there, but none of them worked. So, this appears to be a different problem. Please note that this occurs during the build of my solution and not referencing (explicit) NuGet packages in any way.

Finally, if it helps, I have a solution that demonstrates this issue here: https://github.com/Mike-EEE/Stash/blob/master/VS2017.Multi/VS2017.dotNetFramework.sln

Mike-E
  • 2,477
  • 3
  • 22
  • 34
  • 6
    You should target .NET 4.6.1 or above. – Alois Kraus Jun 18 '17 at 18:55
  • 1
    I have tried your sample project and it seems that project references do not work yet with VS 2017.2 with .Net Standard referenes. Add the library as a normal file reference and add to your Console App the NETStandard.Library Nuget package then it will work. – Alois Kraus Jun 18 '17 at 19:43
  • 3
    It *seems* that way or it *is* that way? This certainly *seems* like such an obvious and primary scenario that there should be working code somewhere that demonstrates this. It is very surprising to say the least that nothing can be found. Adding a direct file reference might work for one configuration (Debug), but what about others (Release, etc.)? That is the benefit of using a project reference. If you can find a GitHub issue that declares this due to a known issue as such and present it as an answer I will mark it. Everything I have seen says that this should "just work" as described. – Mike-E Jun 18 '17 at 20:35
  • You can add to the reference path a $(Configuration) for .e.g Debug/Release builds to get a Configuration agnostic csproj file. But VS will usually squiggle it because it cannot properly recognize this one although it works. I agree that .NET Core Tooling is still a mess but it will become better with every release. – Alois Kraus Jun 18 '17 at 21:22
  • I'm glad to see I am not the only one bamboolzed by this new tooling. :) Even after reading the compatibility matrix several times I still did not accurately understand. Please see the accepted answer by @martin-ullrich. – Mike-E Jun 19 '17 at 09:38

2 Answers2

39

.NET Framework 4.5 only supports using .net standard libraries targeting .NET Standard 1.0 or 1.1. Since your library targets 1.6, the tooling does the right thing here and errors out (since your library may use APIs not available in .NET Framework 4.5). If you published the library as NuGet package and consumed it via a package reference, the package restore would error out as well (with an error saying that the package is incompatible).

There is some confusion about which .NET Standard version a .NET Framework version supports especially since there is preview tooling available ("2.0") that changes these versions. The ".NET platforms support" table in the documentation therefore contains two lines about the supported versions. In your case however, both versions limit .NET Framework 4.5 to .NET Standard 1.1.

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • OK! Confusion is indeed the word of the day here, Martin. Thank you for your insight and explanation. My understanding is that chart represented the maximum framework allowed, when in fact it is the **exact** framework allowed. I set the Common project to `netstandard1.4` and dotNet.Framework to `net461` and both projects now compile as expected. Thanks again! – Mike-E Jun 19 '17 at 09:33
  • 1
    Ah... since I can no longer edit the above comment, I wanted to be sure and clarify that `net461` was always my intended target, but my question above uses `net45` as that is a lower framework and felt that would simplify things. But as we are clearly finding out, nothing is simple here. ;) – Mike-E Jun 19 '17 at 09:44
  • 1
    Yeah i had a look in your GitHub repo and it was `net45` as well, but i didn't try to build it. If you hit other issues as well it's probably better to open new questions.. – Martin Ullrich Jun 19 '17 at 09:47
  • I know this is an old post so maybe things have changed. But I created a .NET Standard dll (2.0), and was able to run it with a .net framework 4.5 console app. I just needed to reference netstandard.dll (along with my custom .NET standard dll) in the console app. – benmartin101 Jan 10 '20 at 01:10
  • If you use dll files, anything is possible until it used an unavailable API.. – Martin Ullrich Jan 10 '20 at 08:28
  • **2022:** in case anyone is Googling, things have progressed since 2017. Whilst **4.5 remains the same** as per the fine answer above, **.NET Framework 4.6.1 --> 4.8** which are under the **.NET Standard 2.0** baseline can utilise **.NET Standard 2.0** libraries. –  Jun 08 '22 at 03:58
9

for .net framework projects to be compatible with .net standard libraries you must acquire the NETStandard.Library from the nuget.
Now i cannot find any official resource that states exactly why this is a must, but from what i understand the NETStandard.Library has the necessary links to make a map from .NET Standard API's to .NET Framework.
If you want more info i suggest to read the official docs of NET Standard.

  • 1
    I have read the docs and especially [the matrix of supported framework versions](https://learn.microsoft.com/en-us/dotnet/standard/library), and 4.5 certainly claims to be supported. Yet, no mention on **how** to do this, with working and/or reference code. Any pointers to this would be greatly appreciated. – Mike-E Jun 18 '17 at 20:29
  • Altough this doesn't answer the question, I think this point is very interesting and also hard to find information about. "for .net framework projects to be compatible with .net standard libraries you must acquire the NETStandard.Library from the nuget." – Jim Aho Mar 22 '18 at 10:59
  • This is arguably obsolete info. _"for .net framework projects to be compatible with .net standard libraries"_ - the projects must be targeting a .NET Framework version under one of the .NET Standard _baselines_. See _[.NET Standard Versions](https://github.com/dotnet/standard/blob/v2.1.0/docs/versions.md)_. In other words .NET Framework 4.5 --> 4.6.1. To see how each maps to a particular .NET Standard baseline visit the link mentioned. –  Jun 08 '22 at 04:03