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?

3
  • 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. Commented Dec 12, 2022 at 8:30
  • Can I add a reference to a .NET Framework DLL from a .NET 6 project? Commented Dec 12, 2022 at 8: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. Commented Dec 12, 2022 at 13:07

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.