3

I have a net6.0 SDK-style project and I want to use a library which seems to be only released to .net framework. After I added the reference, I got a warning as below. And for sure I can't use any classes from the lib in my code.

Package 'xxx' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net6.0'. This package may not be fully compatible with your project.

I'm new to C#. Does it mean that each lib only works for specific .net versions? Library developers declare and compile their code for the supported .net versions and publish to nuget. When we refer to a nuget lib, nuget will try to find the dll which matches our .net version and throw the above warning if not found.

That is to say, to be compat with the library, I have to downgrade my project to net472?

  • old .NET 4 framework is not backwards compatible with new .NET Core \ .NET 5+ framework. So it might or might not work, as warning suggests, depending on what apis exactly that library uses. – Evk Dec 12 '22 at 08:30
  • [Can I add a reference to a .NET Framework DLL from a .NET 6 project?](https://stackoverflow.com/questions/70005465/can-i-add-a-reference-to-a-net-framework-dll-from-a-net-6-project) – KekuSemau Dec 12 '22 at 08:57
  • .net6 has pretty decent backwards compatibility with .net4. But it isn't perfect, you may need a nuget package, you may have to target .net60-windows and give up on Unix compatibility, you might get a NotImplementedException, it might just not compile. The NIE makes it painful, thorough testing required. – Hans Passant Dec 12 '22 at 13:07

2 Answers2

1

Does it mean that each lib only works for specific .net versions?

It is more nuanced than that; usually, libraries are perfectly happy with lower versions - meaning: if I target net5.0 from a library, and your application is net7.0: that's fine (with the caveat that even minor revisions can - but rarely - break something, if it was depending on functionality that has changed, perhaps because it was a bug - or some other implementation detail).

However, there is a hard break between .NET Framework and .NET Core (now just .NET); a .NET Core / .NET application cannot reliably consume a library that only targets .NET Framework, as .NET Framework is a fundamentally different framework than .NET Core / .NET.

In this scenario, libraries can do a few things to help you:

  • they can target some variant of .NET Standard, which is the subset of APIs that should work correctly on both .NET Framework and .NET Core / .NET (and other platforms that implement .NET Standard, such as Unity, Mono, Xamarin, etc as)
  • they can multi-target, which is to say: in the nupkg they can include multiple assemblies, perhaps a net472 target, a net6.0 target, and maybe one or two others

(the second option is more common than the first; .NET Standard is fading into the background now)

If the library you're using only supports .NET Framework, then indeed that won't work from .NET Core / .NET; you have three options:

  1. limit yourself to .NET Framework (I do not recommend this option)
  2. see if the library can be updated to use .NET Core / .NET (by contacting the maintainers, and possibly even proposing the changes yourself)
  3. use a different library
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

Yes, that is correct. Each library is typically compiled for specific versions of .NET.

If you want to use this library, you will need to downgrade your project to target one of the supported .NET Framework versions.

Mikolaj
  • 187
  • 2
  • 17
  • 1
    Or, if it's worth the time required (and it's actually possible, no mention of what the .NET Fx library deals with), build a .NET Standard 2.0 Library that acts as bridge – Jimi Dec 12 '22 at 08:33
  • @Jimi netstandard is rarely a good answer, to be honest; multi-targeting netfx and modern .net (perhaps with preprocessor directives on any platform delta) is almost always preferable – Marc Gravell Dec 12 '22 at 10:02