Multi targetting net461 and netstandard - netstandard dependencies required even in net461 consumer

338 Views Asked by At

I'm working on a class library that I've multi-targetted to both net461 and netstandard2.0

One of the dependencies of this class library is Microsoft.ApplicationInsights

When it was targeting just net461, I could add a reference to Microsoft.ApplicationInsights (v2.4.0) via package manager console, or nuget ui, and it would add itself as a dependency.

Once I've multi-targetted the csproj:

 <Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
  </PropertyGroup>

</Project>

... if I try to add a reference, it asks me to accept license agreements for many, many dependencies. Of course, I duly did so.

My issue comes when I package this class library as a nuget package.

Even if my consuming application targets net461, when I install this package, I am prompted to install all the netstandard dependencies - even if my consuming application doesn't target netstandard.

Is there a way to stop my net461 targetted package requiring all the dependencies for netstandard?

1

There are 1 best solutions below

1
On

Have you tried using conditions in the project file to make some dependencies target framework specific? I've had similar sounding problems, though not with creating nuget packages, and this helped.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
    <Reference Include="DependencyA" />
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
    <PackageReference Include="DependencyB" Version="1.0.0" />
  </ItemGroup>

</Project>

Another idea would be to look at existing open source projects out there and see how they're solving it. Though finding one using Microsoft.ApplicationInsights may be trickier.