0

I am trying to reference from a .NET Core 3.1 project, a NuGet package that targets only net40 via the NuGet compatibility shim. The package is added to my project, however the net40 dependencies are not.

The package is structured as:

lib\
  net40\
    AssemblyA.dll

nuspec: 
  <dependencies>
    <group targetFramework="net40">
      <dependency id="PackageB" version="1.0" />
    </group>
  </dependencies>

Visual Studio's Package Manager lists the dependencies, but when installed, the dependencies are not listed by VS' Preview Changes window and are indeed not installed. PackageB also targets net40.

It does work if I:

  • Include the dependencies in an "Any" (blank) dependency group as well as under net40
  • Remove the net40 dependency group and list the package directly under <dependencies> as a flat list
  • Remove the net40 under lib\ and use a flat list

These are not ideal as it obfuscates the true nature of the targets frameworks. The last two produce NU5128 on pack. For future reference, I should mention that it's required to remove the dependencies from the local cache for even these scenarios to work (surely a bug?).

Any ideas on how to pull dependencies from such packages? Is this simply not supported? A good test example of this is the "Polly.Net40Async" package.

(VS: 16.6.5, dotnet: 3.1.302, PackageReference, Windows 10)

Terrence
  • 747
  • 1
  • 10
  • 27
  • 1
    It's a bug in NuGet, but it hasn't got enough upvotes to be prioritized: https://github.com/NuGet/Home/issues/5957 – zivkan Jul 31 '20 at 19:04
  • @zivkan, Thanks for the link, indeed it looks like a bug. It appears the keyword I was missing in my fruitless googling on this was "AssetTargetFallback". (aside) Who would downvote this question??? – Terrence Aug 03 '20 at 16:35

1 Answers1

0

I am trying to reference from a .NET Core 3.1 project, a NuGet package that targets only net40 via the NuGet compatibility shim. The package is added to my project, however the net40 dependencies are not.

The nuget package only targets to net40 which means the package is used for net framework 4+.

And by default, Net Core projects cannot use this type of nuget unless the package and dependencies are listed as supporting Net Core or Net Standard.

==========================================

Also, you can notice the info from the Polly.Net40Async nuget package:

enter image description here

group targetFramework="net40" means that if your project targetframework version is 4+, it will install the listed dependencies.

So you should not install this type of packages into Net Core 3.1 project. And it is designed by the author.

Besides, if you change the sub folder of lib in the nuget package to any, you can add the dependencies into the Net Core project.

After all, any means it targets to any framework versions--net core, net standard, net framework. And you just need to remove the condition of the dependency (contention on Net Framework 4+).

====================================

Add more detailed info

Update 1

Actually, Net Core projects can install some nuget packages which only targets to Net Framework.

In my side, the package can be installed in the Net Core project.However, there is a warning which shows it may not be fully compatible with your Net Core project. Although you can use it, there are still some problems just not encountered in special situations.

For the dependencies, since your project targets to Net Core rather than Net Framework, the dependencies will not be installed automatically along with the main package. But you can manually install these dependencies separately through Nuget Package Manager UI.(search them and then install them one by one).

And if condition group targetFramework="net40" is met, it will install these dependencies automatically along with the main nuget package. But since your project targets to Net Core, it will not install them automatically.

As a suggestion, you could search these dependencies on the Nuget Package Manager UI, and then manually install them separately.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
  • Thanks for the response... I respectfully disagree. Support for taking a .NET Framework reference from a .NET Core project is explicitly supported. In my specific case, the Framework API's I'm using from Core are 100% compatible, however, the rest of the API may not be. I feel that it would be wrong to ask a package owner to falsely advertise a package as fully supporting either Core or 'any' target as a workaround to this bug. For now as a workaround, the package's dependencies can be directly referenced by the Core project. – Terrence Aug 03 '20 at 16:44
  • `Sorry for not providing enough detailed info first. ` Actually, net core projects can install net framework nuget packages. However, it will turn out a warning: compatibility problem.Although it can be used, there may be problems in some special situations. And since your project targets to `Net Core` rather than `net40`, the condition is false and it will not install the dependencies automatically along with the main project. So you will not see the dependencies on your project as you described before. – Mr Qian Aug 04 '20 at 09:47
  • `As a suggestion`, you should search them one by one on the` Nuget package manager` and then install them manually. And then your project can find all of them. – Mr Qian Aug 04 '20 at 09:51
  • I have supplemented the detailed info in my answer and you can check it:) – Mr Qian Aug 04 '20 at 09:51