5

I have an application that we are going to upgrade from .net framework 4.6 to .net 6. However one class library has to be run in .net 5 because of limitations in a Linux version that we have to use. So I wonder if it is possible to use the .net 5 library with .net 6?

Fred
  • 1,129
  • 1
  • 14
  • 35
  • .NET 5 is already out of support. `a class library of it has to be run in .net 5` no it doesn't. The .NET Core (.NET 5 is .NET *Core* 5) was announced years ago. .NET 5 was a "current"-single year release. The Long-Term-Support release is 6, which is supported until 2024 – Panagiotis Kanavos May 23 '22 at 08:26
  • Does this previously asked question with [this answer](https://stackoverflow.com/questions/56027997/can-i-add-a-reference-to-a-net-framework-dll-from-a-net-5-project) help – Mircea Matei May 23 '22 at 08:26
  • *Why* does that class library need to remain on .NET 5? All you need to upgrade is to change `net5.0` to `net6.0`. Yes, you can use a .NET Core 3.1 or 5 library in .NET Core 6, but why do it? – Panagiotis Kanavos May 23 '22 at 08:26
  • I updated my explanation because I realized it was hard to understand. Hopefully it is easier to understand now. @PanagiotisKanavos – Fred May 23 '22 at 08:34
  • The question was clear from the start. Why do you insist on using an unsupported runtime? `we have to use` doesn't make .NET 5 any less unsupported. Unsupported means no fixes, no new packages, and third-party packages will start removing support for .NET 5 soon, if they haven't already. Using a .NET 5 library in a .NET 6 project shouldn't be a problem in itself BUT can lead to problems if that class library requires older versions of other libraries or NuGet packages. You'll have to try using that library and see if it causes any problems. You'll have to migrate to .NET 6 sooner or later – Panagiotis Kanavos May 23 '22 at 12:18
  • @PanagiotisKanavos I don't know how to explain it any more clearly than that .net 6 does not work with the version of Linux that we have to use. The Linux version will be upgraded in the future but that does not help us right now. However. Good to hear that it will work with .net 5 and6 "mixed". – Fred May 24 '22 at 05:57
  • It's not mixed, it's still .NET 6. If you create a .NET 6 application and use a .NET 5 library, the application will still need the .NET 6 runtime. If your Linux distro doesn't support .NET 6, the application may not run or have problems. If you create a self-contained executable, it may run until it tries to use a system function that's missing. Or there may not be any problems - just that MS won't fix any problems encountered on that distro – Panagiotis Kanavos May 25 '22 at 08:34
  • You can target multiple runtimes in your csproj, both `net6.0` and `net5.0` if you have to, to produce executables that can run in both runtimes – Panagiotis Kanavos May 25 '22 at 08:37

1 Answers1

6

I tested experimentally on a simple case. The .NET6 app referencing .NET5 library could be built without any warnings or errors and ran without runtime problems:

Referencing a .NET5 library from a .NET6 app

Some related quotes from Microsoft Docs:

If you're using libraries to break down an application into several components, we recommend you target net5.0 or net6.0. For simplicity, it's best to keep all projects that make up your application on the same version of .NET. Then you can assume the same BCL features everywhere. (...)

To me, this sounds like a good practice to have everything on the same version (5 or 6), but not like a strong requirement.

I'm not 100% confident about edge cases, however. I couldn't find any confirmation of compatibility. At the moment, I would hesitate running a complex production app built from components mixing 5 and 6.

Paweł Bulwan
  • 8,467
  • 5
  • 39
  • 50