Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a .Net Standard 2.1 DLL in .Net Framework 4.8?

Tags:

c#

.net

I have a project that is targeted to .Net Framework 4.8. Now, we need to use a 3rd party dll. The problem is that the dll is targeted to .Net Standard 2.1.

Is there a way to use this dll in the .Net Framework 4.8 project?

I added the dll, get this error:

Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

I searched, seems it should work directly. Maybe I need to add a reference? Not sure what needs to do now.

Thanks

like image 686
urlreader Avatar asked Aug 25 '20 22:08

urlreader


People also ask

Is .NET standard 2.0 compatible with .NET Framework?

Broad platform support. . NET Standard 2.0 is supported on the following platforms: . NET Framework 4.6.

Can I use .NET standard in .NET Framework?

NET Standard will run on any . NET Standard compliant runtime, such as . NET Core, . NET Framework, Mono/Xamarin.

Can a .NET standard project reference a .NET Framework project?

In Visual Studio for Mac if you try to reference a.NET Framework project from a . NET Standard project. the IDE won't let you do it due to incompatible target frameworks, but in Visual Studio 2019 on Windows, you can add that reference.


1 Answers

You cannot consume a .Net Standard 2.1 assembly in any .Net Framework Version because the .NET Framework (even the last ever version, 4.8) does not implement .Net Standard 2.1.

The .Net Standard .NET implementation support matrix at https://docs.microsoft.com/en-us/dotnet/standard/net-standard does not contain an entry for 2.1 under "Framework".

More details on the decision by MS: https://devblogs.microsoft.com/dotnet/announcing-net-standard-2-1/

Extract:

Given many of the API additions in .NET Standard 2.1 require runtime changes in order to be meaningful, .NET Framework 4.8 will remain on .NET Standard 2.0 rather than implement .NET Standard 2.1. .NET Core 3.0 as well as upcoming versions of Xamarin, Mono, and Unity will be updated to implement .NET Standard 2.1.

Library authors who need to support .NET Framework customers should stay on .NET Standard 2.0. In fact, most libraries should be able to stay on .NET Standard 2.0, as the API additions are largely for advanced scenarios. However, this doesn’t mean that library authors cannot take advantage of these APIs even if they have to support .NET Framework. In those cases they can use multi-targeting to compile for both .NET Standard 2.0 as well as .NET Standard 2.1. This allows writing code that can expose more features or provide a more efficient implementation on runtimes that support .NET Standard 2.1 while not giving up on the bigger reach that .NET Standard 2.0 offers.

Solution: The easiest way would be to convince the 3rd party to backport/multi-target to support netstandard2.0 as well. Alternatively, you can switch to targeting .Net 6 (LTS), .Net Core 3.1 (LTS), or Mono instead of Framework 4.8 in your project. (The support matrix might change in time, this was the supported list at the time of writing).

UPDATE:

Another possible workaround: You might also attempt some trickery with .NET Framework compatibility mode. I had no idea you could do this. You could, for example, make a "wrapper" project that targets standard and references both your .Net Framework 4.8 code and the 3rd party library, unless the 4.8 part uses WPF - perhaps there are other pitfalls in unsupported scenarios as quoted below. It sounds convoluted to me, but if you have no other option, it might be worth a shot. From the same original site:

Starting with .NET Standard 2.0, the .NET Framework compatibility mode was introduced. This compatibility mode allows .NET Standard (*) projects to reference .NET Framework libraries as if they were compiled for .NET Standard. Referencing .NET Framework libraries doesn't work for all projects, such as libraries that use Windows Presentation Foundation (WPF) APIs.

(*) - Also .Net Core (elsewhere in the document/page)

like image 136
Alexandru Clonțea Avatar answered Oct 23 '22 06:10

Alexandru Clonțea