Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify different dependencies for different versions of the .NET framework in a custom NuGet package?

Tags:

c#

.net

nuget

I'm trying to create a NuGet package which has a dependency on System.Net.Http (need the HttpClient). For framework version 4.5.1, this assembly is a part of the BCL. Hoewever, in 4.0 it is not. I believe it having compiling correctly with the proper conditional statements in the csproj.

The problem I'm currently wrestling with is that when I reference this package in a 4.5.1 project, it pulls in the dependency on Microsoft.Net.Http. I really only want to depend on Microsoft.Net.Http for net40.

Here's my nuspec file:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>MyApp</id>
    <version>$version$</version>
    <title>MyApp</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Description</description>
    <releaseNotes>Initial release</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <dependencies>
      <group>
        <dependency id="Newtonsoft.Json" version="8.0.2"/>
      </group>
      <group targetFramework="net40">
        <dependency id="Microsoft.Bcl" version="1.1.10" />
        <dependency id="Microsoft.Bcl.Build" version="1.0.14" />
        <dependency id="Microsoft.Net.Http" version="2.2.29" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="bin\release\**\MyApp.dll" target="lib" />
  </files>
</package>

In VS, the NuGet package shows this:

.NETFrameworkVersion=v4.0

But again, I'm those dependencies are also being pulled in when using a project with target framework 4.5.1. Which I don't want. Any help is appreciated.

like image 319
brendonparker Avatar asked Feb 10 '16 14:02

brendonparker


1 Answers

Need to be more specific when defining dependencies by framework version.

<?xml version="1.0"?>
<package>
  <metadata>
    <id>MyApp</id>
    <version>$version$</version>
    <title>MyApp</title>
    <authors>Me</authors>
    <owners>Me</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Description</description>
    <releaseNotes>Initial release</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <dependencies>
      <group targetFramework="net451">
        <dependency id="Newtonsoft.Json" version="8.0.2"/>
      </group>
      <group targetFramework="net40">
        <dependency id="Newtonsoft.Json" version="8.0.2"/>
        <dependency id="Microsoft.Bcl" version="1.1.10" />
        <dependency id="Microsoft.Bcl.Build" version="1.0.14" />
        <dependency id="Microsoft.Net.Http" version="2.2.29" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="bin\release\**\MyApp.dll" target="lib" />
  </files>
</package>

Both versions of framework

Typical... after struggling with this for hours, I come up with the answer minutes after posting the question.

like image 73
brendonparker Avatar answered Oct 22 '22 08:10

brendonparker