<PackageReference Include="Some.Framework.Package" Version="1.0.0" NoWarn="NU1701" />
The fix (for real this time)
Recompile the assembly to target .NET Standard 2.0, and package and distribute that through NuGet.
This is a question with a complex answer.
Let me blunt. It is highly likely the answer to your question will be no, it can't be done, but that is because you have a very specific library in mind. The real answer is that it depends on the library in question, but chances are mostly geared towards this not working.
Yes, you can load .NET Framework assemblies into .NET Core 5 and 6.
However, depending on what that library does, and probably more important, what dependencies it has aka other libraries it wants to drag with it, it might not work properly for any sizable complex library.
There are classes that doesn't exist in .NET 5+, or even just single method overloads or properties. Depending on the exact parts of .NET Framework you access, it may in fact just be missing outright in .NET 5+.
There is more information here.
Your best option is probably to try to:
Re-compile the library for your particular .NET version (be it 5 or 6)
Re-compile the library targeting .NET Standard 2.1, as .NET 5+ is compatible with that
However, you might in fact just get into the exact same problem with that re-compile as you will have to deal with those changed bits. But, then at least you would have more knowledge about what would and wouldn't work, and you have a chance to fix it.
If it's not your library to change, either replace it, or try and hope for the best.
In general, no you can't. They're not really designed to be compatible. .NET Core and the subsequent .NET 5 and 6 are based on a complete re-write of .NET.
There may be some chance of things which work using the .NET Framework Compatibility mode but it's far from guaranteed.
If you want reliable, cross-framework compabitility for your libraries between .NET Framework and .NET Core / 6 you can try to target your libraries at .NET Standard
You can have a .NET 6 Core application calling into a .NET Framework 4.8 dll. It took me a while to figure out how to do it, but it can be done. I have created both console and web .NET 6 Core applications which call into some of my legacy .NET Framework 4.8 dlls. The process is as follows:
Create or open your .NET 6 Core solution in Visual Studio 2022 and add your legacy .NET Framework 4.8 dll to your .NET 6 Core solution.
Add your legacy .NET Framwork 4.8 dll as a reference to your top level .NET 6
Core Console or Web project.
Delete the "obj" and "bin" directories from your top level project via file explorer.
From the Visual Studio 2022 top level menu, select "Tools > Command Line > Developer Command Prompt". This should open a command line window with the current directory changed to that of your solution.
Run the command "dotnet restore" in the command window. This will re-create your "obj" directory and place a "project.assets.JSON" file into the "obj" directory which will reference your recently added .NET Frameowork 4.8 dll. If you fail to do this you will get a runtime error when you run up your application.
Rebuild and rerun your application. Your .NET Framework DLL should now load with no problems.
I'm not sure if the removal of the "obj" and "bin" directories is always essential, however I have found that if you don't remove them, occasionally the "project.assets.JSON" file isn't generated properly and running fails. Removing them then re-running the command solves the problem.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.