Kevin
Kevin

Reputation: 1798

Can NET Framework 4.7.2 apps load at runtime NET Core 5.0 library DLLs?

I'm starting to port some of my NET Framework 4.7.2 class libraries to NET Core 5.0. The host Framework app dynamically discovers and runs Assembly.Load on the NET Core library ok. But when I try to do a Assembly.GetType(NET50Namespace.Classname) operation on the loaded assembly, it fails and returns null with the error:

{"Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.":"System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}

It looks like it is trying to load the NET Core 5.0.0.0 runtime, which I do have installed. I checked with the console command dotnet --list-runtimes and both the NETCore and WindowsDesktop runtimes are installed ok:

c> dotnet --list-runtimes

Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

I'm stuck.

Q1. Is it legal/possible for a Framework 4.7.2 app to dynamically load and run a NETCore 5.0 DLL? I was trying to port small pieces of my app to NET50 as a first step. But it is not working, and I might be trying to do the impossible.

Q2. If it is ok for Framework to load/call NET50 DLLs, what am I doing wrong, and how can I make the GetType call work so that I can instantiate and call the NET50 class library?

Thank you.

Upvotes: 3

Views: 5408

Answers (2)

Dave R
Dave R

Reputation: 1

One other thing to keep in mind. If you have installed Microsoft .NET SDK 6 or 7, it does not matter your target framework (5.0 in your case). dotnet publish will always use the most recent version of the SDK. This can lead to unexpected problems because you now have incompatible dlls or even dlls you don't need. Try creating a global.json file to indicate the SDK you want to use during publish. https://learn.microsoft.com/en-us/dotnet/core/versions/selection

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1064234

"No"

Fundamentally, that is not a supported scenario, and there are hard breaks between .NET Framework and .NET Core (which essentially became .NET 5).

If it helps, .NET 4.7.2 "kinda mostly sort of" supports .NET Standard 2.0, which .NET 5 also implements (correctly), so if your library can target .NET Standard 2.0 it could potentially be loaded by both a .NET 5 and .NET Framework 4.7.2 application. You could also multi-target the library with multiple TFMs, for example net472 and net5.0 (perhaps with some netstandard too, for good measure)

However, quite honestly: it is only going to get harder and harder to keep working with .NET Framework; if at all possible, you should prioritise moving to a more recent .NET version instead (such as .NET Core 3.1 or .NET 5, at time of writing).

Upvotes: 4

Related Questions