3

I have created a NuSpec file for my .NET Project as follows:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>Author</authors>
    <description>My Project</description>
    <owners>Me</owners>
    <dependencies>
    </dependencies>
  </metadata>
</package>

My project also has two NuGet provided dependencies, these being:

<package id="Autofac" version="3.5.2" targetFramework="net451" />
<package id="Autofac.Extras.NLog" version="1.2.3" targetFramework="net451" />

When I create the NuGet package for my project using this NuSpec, NuGet is smart enough to pull these addtional dependencies in. When I install my NuGet package in a new Project, I also get the Autofac and Autofac.Extras.NLog dependencies too, referenced and automatically inserted into the packages.config for my new Project.

However... the version of Autofac I get is wrong. Rather than version 3.5.2 I get version 2.6.1.841:

<package id="Autofac" version="2.6.1.841" targetFramework="net451" />
<package id="Autofac.Extras.NLog" version="1.2.3" targetFramework="net451" />

Now, Autofac.Extras.NLog has a dependency of ≥ 2.2.4.900 (at time of writing). I have two questions:

  • It looks as though NuGet is first fulfilling the Autofac.Extras.NLog Autofac dependency by installing Autofac 2.6.1.841. When it then comes to fulflling my project's Autofac depdency, it is seeing that Autofac is already installed and therefore does nothing. How can I make NuGet resolve the Autofac dependency to version 3.5.2?
  • Even though NuGet is resolving the 'wrong' NuGet depdendency (at least for my purposes), why is it resolving to 2.6.1.841 rather than 2.2.4.900, which is the minimum version specified in the Autofac.Extras.NLog dependency?
Holf
  • 5,605
  • 3
  • 42
  • 63

3 Answers3

0

you can restrict artifact version to be referenced by specifying version number in a pair of square brackets. please find sample below

<package id="Autofac" version="[3.5.2]" targetFramework="lib/net45" />

edit your packages.config file with the above line , and see if that works ..!!

prudviraj
  • 3,634
  • 2
  • 16
  • 22
  • This works fine if you do it in the consuming project. However, I don't think it works if you put it in the project which is packaged by NuGet. I don't want the consumers of my project to have to add this NuGet Reference themselves, or thereafter doctor it to be at Version 3.5.2. – Holf May 27 '15 at 10:53
0

A solution is to add these dependencies to the NuSpec file:

<?xml version="1.0"?>
<package>
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>Author</authors>
    <description>My Project</description>
    <owners>Me</owners>
    <dependencies>
      <group targetFramework="net451">
        <dependency id="Autofac" version="3.5.2"/> <!-- EXTRA DEPENDENCY -->
      </group>
    </dependencies>
  </metadata>
</package>

It would still be good to know if there's a solution where you don't have to maintain the NuSpec file every time there are dependency version changes in the project you're packaging.

Holf
  • 5,605
  • 3
  • 42
  • 63
0

Looks like Nuget 3.5 (now in Beta) finally solved this issue. I tested on my project and the dependencies were calculated correctly (Nuget 3.4 didn't calculate them correctly).

Nuget download page

This is the pull request: https://github.com/NuGet/NuGet.Client/pull/632/files

Alon Catz
  • 2,417
  • 1
  • 19
  • 23