简体   繁体   中英

NuSpec-generated NuGet file is resolving the wrong version of a dependency - how can I force it to do otherwise?

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?

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 ..!!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM