Neeraj
Neeraj

Reputation: 19

Using a higher version (.NET Framework version 4.7.2) library in a lower version class library project (version 4.5.2)

I have a class library project which is targeting .NET Framework version 4.5.2 and I want to use an assembly of .NET FW version 4.7.2 in this project.

I cannot downgrade or upgrade the target framework versions of any of the projects due to other dependencies.

1 solution I found is to load the assembly dynamically using reflection and then use it but I am unable to find any MSDN link for this.

Any suggestions or other solutions to solve this problem?

Upvotes: 1

Views: 2936

Answers (2)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131706

You can't and it wouldn't make sense as a library depending on 4.7 features would fail if it tried to run on the 4.5.2 runtime.

That doesn't really matter though because you are probably not using 4.5.2 anywhere. .NET 4.x runtimes are binary replacements. If you install 4.7 on a machine it will replace any earlier versions. If you installed 4.7 SDK on your machine you're already running 4.7.

The only supported desktop Windows version, Windows 10, comes with .NET 4.7, so any clients you target probably already have 4.7. If they run unsupported versions a) you can charge them extra for working on unsupported OSs and b) probably got updated through Windows Update anyway.

4.5.2 going out of support in 2022

Anything below 4.6.2 is going out of support on April 2022. That's less than a year away. You should be planning to update legacy projects to 4.6.2 at least, not trying to use current libraries on legacy runtimes.

So even if you target a supported Windows Server version that comes with .NET 4.5, you should be looking to upgrade.

.NET Standard and why it didn't work

Theoretically, using .NET Standard would allow a .NET Standard library to run on earlier versions through compatibility libraries. This never worked because of versioning conflicts even between compatibility libraries. The .NET Standard docs page warns that

While NuGet considers .NET Framework 4.6.1 as supporting .NET Standard 1.5 through 2.0, there are several issues with consuming .NET Standard libraries that were built for those versions from .NET Framework 4.6.1 projects. For .NET Framework projects that need to use such libraries, we recommend that you upgrade the project to target .NET Framework 4.7.2 or higher.

If you want to use a NuGet package or library targeting .NET Standard 2.0, you should target 4.7.2

TLS 1.2

TLS 1.2 support is another reason why you shouldn't keep using anything less than 4.6.2. By now, almost every web service requires TLS1.2 or higher. .NET 4.6 was the first version that used TLS1.2 automatically. In previous versions you had to hard-code the TLS version, preventing your code from using any newer versions. This means that if a .NET 4.5.2 version tries to contact a service supporting TLS 1.3 it either downgrade to TLS 1.2 or fail to connect, if the server demands TLS1.3.

Starting with 4.6, .NET will use the best encryption available by the OS. In Transport Layer Security (TLS) best practices with the .NET Framework the recommendation for 4.5.2 is :

We recommend you upgrade your app to .NET Framework 4.7 or later versions.

Upvotes: 1

It's not possible to do what you want, and if you think about it for a moment you'll understand why: if the newer library uses functionality from the new framework it was built against (likely), trying to load it into the old framework will fail.

Your .NET Framework 4.5.2 project is legacy code at this point. If you choose to remain on that version (and it is always a choice, regardless of what you might claim), you are choosing to constrain yourself to similarly legacy libraries.

Upvotes: 0

Related Questions