'When do target frameworks need to match between nuget and consuming project and when not?

There are nuget packages where dlls only exist for one target framework, like this one:

- lib
  - net40
      SolidWorks.Interop.sldworks.dll
      SolidWorks.Interop.swconst.dll
      SolidWorks.Interop.swpublished.dll

No matter which target framework I choose for a consuming project - e.g. net48, netcoreapp3.1, net5.0-windows, net6.0-windows - it will always use the content from the net40 folder.

I recently created a nuget from a project with the above listed four target frameworks, which resulted in the creation of the following folder structure in the nuget package:

- lib
  - net48
      MyDll.dll
  - netcoreapp3.1
      MyDll.dll
  - net5.0-windows7.0
      MyDll.dll
  - net6.0-windows7.0
      MyDll.dll

For compatibility reasons, I must use the net48 version for all my consuming projects targeting net48 or above. So I just removed the netcoreapp3.1, net5.0-windows7.0 and net6.0-windows7.0 folders from my nuget to get

- lib
  - net48
      MyDll.dll

, but then a consuming project using target framework netcoreapp3.1, net5.0-windows or net6.0-windows complains that it can't find the namespace, classes etc..

So in this case, target frameworks between nuget and project do need to match.

When is which? How can I influence this?

My goal is to create a nuget package that contains just one subfolder in lib with dlls inside so that

  • no project with target framework below net48 can work with it
  • all projects with target framework net48 or newer work with it.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source