Search code examples
c#.net.net-standard.net-4.6.1.net-standard-1.4

.NET Framework 4.6.1 application has errors referencing .NET Standard 1.4 class library


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?


Solution

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