3

I've been trying to wrap my head around the differences between the .NET Frameworks, .NET Core and .NET Standard. From what I've been reading, .NET Standard is kind of the lowest common denominator, and that libraries written using .NET Standard should be compatible with code written using the other frameworks.

But I've created a solution with a class library that uses .NETStandard 1.4 and an application that uses .NET Framework 4.6.1, and it appears the application is not able to use that library.

Wherever the application attempts to use a class from the class library, I get errors such as the following.

Error CS0012 The type 'IEnumerator<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Error CS0012 The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Error CS1579 foreach statement cannot operate on variables of type 'HtmlMonkey.HtmlNodeCollection' because 'HtmlMonkey.HtmlNodeCollection' does not contain a public definition for 'GetEnumerator'

Can someone help me round out my understanding of these libraries such that my application can use my class library?

Community
  • 1
  • 1
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • are you using the latest version of VS 2017? (currently 15.4.1) – Martin Ullrich Oct 22 '17 at 15:04
  • @MartinUllrich: Yes, in fact I also have the new VS 2017 Preview installed on the system, although I am currently using the released version. – Jonathan Wood Oct 22 '17 at 15:06
  • 3
    Did you install the `NETStandard.Library` nuget package to the hosting application as well? alternative would be to use `PackageReference` instead of packages.config (NuGet option that affects the first install) - this way the references flow transitively. – Martin Ullrich Oct 22 '17 at 15:07
  • You may want to take a look at https://github.com/dotnet/announcements/issues/31 – Martin Ullrich Oct 22 '17 at 15:08
  • @MartinUllrich: I don't recall manually installing any version of the framework, and I really wouldn't want to as I'm trying to avoid situations like this where incompatibilities are more likely. As indicated, one of my projects is using the .NET Standard library. I would've guessed that this is not possible if the library is not installed on my machine. – Jonathan Wood Oct 22 '17 at 15:11
  • 2
    Have a look at the announcement @MartinUllrich has provided. This is almost certainly due to the mismatch between the different package-restore styles. I encourage you to use the new VS2017 / MSBuild 15 .csproj format in your net461 project ([link](https://blog.nuget.org/20170316/NuGet-now-fully-integrated-into-MSBuild.html#what-about-other-project-types-that-are-not-net-core)). – Kirk Larkin Oct 22 '17 at 15:16
  • Use package reference instead of `packages.config`. Would that help? – Lex Li Oct 22 '17 at 15:17
  • I used the preview version of Visual Studio 2017 to rebuild the solution and projects, and it seems to be working okay. – Jonathan Wood Oct 22 '17 at 15:42
  • No repro, so create a [mcve]. No need to post any code, just the steps (exactly which template etc) – H H Oct 22 '17 at 16:34
  • it is a known bug tracked as issue no #503 in netstandard repo – Jacek Blaszczynski Oct 22 '17 at 17:07

2 Answers2

3

This is a known problem regarding compatibility of netstandard 1.4 and below with .NET 4.6.1 - 4.7 projects. It is tracked in .NET Standard GitHub repo as an issue #503 Referencing NETStandard.Library 2.0.0 in net461-net47 project and only using ns1.4 (or lower) libs doesn't work

Description of the problem and working workaround is below:

NETStandard.Library 2.0.0 package doesn't install netstandard1.x packages in net461-net47 projects. This is because we expected the support package to always be present on net461 and later, but when that support was implemented we dialed it back to only turn on when a netstandard1.5 or later library was referenced.

As a result installing a netstandard1.0-1.4 library in a net461-47 project and referencing NETStandard.Library 2.0.0 package will have missing dependencies.

One workaround is to use the NETStandard.Library 1.6.1 package instead. This still has the dependencies on the individual library packages to bring in the facades.

An alternative workaround is to set ImplicitlyExpandNETStandardFacades=true in the project file. This will enable all the facades for ns2.0 assemblies.

Jacek Blaszczynski
  • 3,183
  • 14
  • 25
0

I don't remember the exact sequence was used to create my original projects. As mentioned in the comments, I had both Visual Studio 2017 and the new Visual Studio 2017 Preview installed on my machine. It's even possible I used different versions to create each project in my solution.

Either way, I used the Visual Studio 2017 Preview to recreate the solution, again with the class library using .NET Standard, and a WinForms application that uses .NET Framework. And it appears to be working just fine. (I did not need to install anything manually with NuGet.)

So not a very clear resolution. I originally assumed it was a more concrete issue related to which frameworks I was referencing. But perhaps I'll leave the question in case anyone gets in a similar situation with the same error messages.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466