相关文章推荐
机灵的皮蛋  ·  Postgres包: psycopg2 ...·  11 月前    · 
安静的小摩托  ·  zookeeper ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-1 does not contain an entry for 2.1 under "Framework". Although very unlikely, the support matrix might change in time; this was the supported list at the time of writing.

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.

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)

Yeah. I spoke too soon. Even though VS2022 allows you to add reference to .net standard 2.1 assemblies, compiling it fails telling me exactly what I don't want to hear. Project 'Test.DAL.csproj' targets 'netstandard2.1'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.8'. :) – dotNET Jan 21, 2022 at 5:51 @AlexandruClonțea: This is not specific to VSTO. I just checked and you can add reference to .net standard 2.1 library in a generic .net fx 4.8 class library. Though the intellisense compiler immediately detects it and stars showing the error. – dotNET Jan 21, 2022 at 13:35 @AlexandruClonțea: And yes, I have read that news. Not sure if they'll stick to it. At one point they were talking about not carry forward WinForms and WPF to .NET 5. And now we have them. JavaScript-based Office add-ins are so hopelessly lame that you can't even set document page size after the library has been in development for 10 years. You can even access non-inline Shapes. It still needs another 10 years to do what VBA could do 20 years ago. – dotNET Jan 21, 2022 at 13:38 @AlexandruClonțea Project references do not necessarily have to result in referencing the assembly or using its code, they might be used only to express any dependency in general. Specifically when you change ReferenceOutputAssembly and SkipGetTargetFrameworkProperties, all you get is the project built into the output, without any checks or references. – IS4 Apr 28 at 9:58 @AlexandruClonțea It does not, I was just replying to the comment that it should not be possible to add a project reference to an incompatible p roject. – IS4 Apr 28 at 22:09

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.