8

Good day Everyone! I have a Class Library project which targets .Net framework 6.0. When I reference this dll into another project which targets .Net framework 4.8, I get the following error message

enter image description here

I will appreciate your help.

1
  • "project which targets .Net framework 6.0" nope, it's just .NET 6. .NET Framework only goes as far as 4.8 before .NET Core 1 -> 3 and then .NET 5 -> 7 (.NET 7 is currently at final RC and not quite production ready yet!)
    – phuzi
    Commented Nov 7, 2022 at 14:20

4 Answers 4

10

The short answer is "you can't". .NET 6 and .Net Framework 4.8 are entirely different beasties, and not compatible with each other.

If you want a library that will work in .NET Framework and .NET, you'll want to look into .NET Standard, specifically version 2.0. It's not got everything in, but it can be referenced from both .NET Framework and .NET 5/6 (and earlier versions of Core).

That being said (h/t PMF), it would be better still if you were able to update the application to be .NET 6. WinForms (which from your screenshot it looks like you're using) is supported in .NET 6, so it should be fairly straightforward to make the change.

3
  • 4
    The better alternative would be to start upgrading the application to .NET6.0.
    – PMF
    Commented Nov 7, 2022 at 11:51
  • 1
    If you're starting the upgrade, might as well go to .NET 7 since it's nearly production ready and should be released very shortly.
    – phuzi
    Commented Nov 7, 2022 at 14:21
  • 1
    @phuzi Sure, but later going from 6.0 to 7.0 should be piece of cake.
    – PMF
    Commented Nov 7, 2022 at 19:29
3

Answer

Use of a .NET 6 DLL from a .NET Framework 4.8 app is not possible. By design, .NET (.NET Core) apps/dlls aren't supported by .NET Framework 4.8. They are different "implementations" of .NET:
https://learn.microsoft.com/en-us/dotnet/fundamentals/implementations
"A .NET app is developed for one or more implementations of .NET ..."

An "implementation" consists of many APIs including the runtime environment (CLR).
https://learn.microsoft.com/en-us/dotnet/standard/clr

From .NET Core 2.1 through .NET 6, many APIs have been added, so .NET 6 has many APIs that the .NET Framework 4.8 doesn't have:
https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-2-1
https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-6

In addition to general API differences, here are some examples of .NET 6 features that aren't supported by the .NET Framework 4.8.

  • .NET 6 supports runtime-related json files such as .runtimeconfig.json and .deps.json that the .NET Framework doesn't support:
    https://github.com/dotnet/runtime/blob/release/6.0/docs/design/features/host-components.md

  • ModuleIntializerAttribute (runtime library, C#9)

  • SkipLocalsInitAttribute (runtime library, C#9)

  • AsyncMethodBuilderAttribute (runtime library, C#10 methods)

  • InterpolatedStringHandler (runtime library, C#10)

  • CallArgumentExpressionAttribute (runtime library, C#10)

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/general


Additional Notes

MSIL code that is generated by a language compiler (e.g. C#) is apparently largely, if not fully, compatible. So, many of the runtime incompatibilities between .NET and .NET Framework appear not to be related to bytecode. Several incompatibilities are related to runtime infrastructure and compiler-generated API calls based on language version. https://github.com/dotnet/runtime/issues/7757

Using a.NET Framework 4.8 DLL from a .NET 6 app is sometimes possible. E.g., if the .NET Framework DLL uses only .NET Standard 2.0 APIs. For example, I've tested using a .NET Framework 4.8 DLL from a .NET 7 app and it worked. I personally, wouldn't use such a scenario for production, but others may decide to. See ".NET Core code can reference existing .NET Framework libraries":
https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-2-0#api-changes-and-library-support)

2

In the scenario when you absolutely need to meet these 2 conditions

  1. Use net4.8 as your application's Runtime/platform
  2. Have your dependency running in .net6 runtime because there is something it can do in .net6 that you can't get in .net4.8 or a library available only in .net6

You can do the following - run 2 different processes.

  1. Wrap .net6 assembly into Web API and communicate via service
  2. Call it via PS script and put result somewhere, where you can read it from.
  3. Communicate via memory mapped files?
  4. Communicate via gRPC?
  5. Use pub/sub mechanism like Redis?

In other words - run 2 different processes and wrap calls you need into endpoint you can listen to. It will be slow or considerably slower. Keep this in mind.

0

.NET 6 and .Net Framework 4.8 are different. They are not compatible with each other. If you want to upgrade. You can refer to the following documents.

Using the .NET Upgrade Assistant to upgrade WPF applications to .NET 6.

Not the answer you're looking for? Browse other questions tagged or ask your own question.