15

I'm using a NuGet package which contains the assemblies for 2 target frameworks: net45 and netstandard1.5

My project is targeting net471 (so compatible with netstandard1.5). When I add the package, it copies the dll from net45 folder. How to force NuGet to use the dll from the standard folder?

The problem with the net45 version is that it needs an older version of a dependency package, the standard dll has no dependency.

zgabi
  • 404
  • 1
  • 4
  • 11
  • Change the target of your project to netstandard1.5? – Heretic Monkey Apr 01 '18 at 13:43
  • 4
    It is not possible to change the target of my project to netstandard. WPF application – zgabi Apr 01 '18 at 14:04
  • This is not supported by NuGet. NuGet will use the assembly that is the closest match. More specific target framework wins. So in this case your project targets .NETFramework so NuGet uses the .NETFramework assembly in the NuGet package. You would need to directly reference the .NET Standard assembly yourself in the project. – Matt Ward Apr 01 '18 at 15:00
  • You should discuss with the package owner(s) to learn why. I have a few public NuGet packages, like [#SNMP](https://github.com/lextm/sharpsnmplib) and its multiple target platforms, like `net45` and `netstandard13`, does behave differently (so users cannot use the assemblies interchangeably). – Lex Li Apr 01 '18 at 19:36
  • 1
    This is a huge problem with System.Net.Http. For a NuGet package that supports netstandard1.3 and netstandard2.0, installed under a project targetting .NET 4.6.1, it always chooses the netstandard2.0 project, which is very bad, because .NET 4.6.1 explodes and dies looking for System.Net.Http version that doesn't exist because of the use of netstandard2.0. So I need to explicitly set the NuGet package to target netstandard1.3 (instead of netstandard2.0) within a project that targets .NET 4.6.1 for this work right. Must I manually alter the project reference to the 1.3 DLL? – Triynko Sep 18 '19 at 22:43

1 Answers1

12

How to choose target framework from Nuget package

According to the official document Matching assembly versions and the target framework in a project:

When NuGet installs a package that has multiple assembly versions, it tries to match the framework name of the assembly with the target framework of the project.

So, just as Matt said: "NuGet will use the assembly that is the closest match. More specific target framework wins.", NuGet will install the .net framework assembly to your .net framework project.

To resolve this issue, you can use Matt suggestion, directly reference the .NET Standard assembly yourself in the project or you can download that nuget package manually, set it to the local feed, open it with NuGet Package Explorer, delete the folder net45 under the lib folder, install that package from local feed, then nuget will use the dll from the standard folder.

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135