Search code examples
c#visual-studio.net-corenugetnuspec

nuget not resolving dependency using what specified in nuget package for Microsoft.Extensions.Logging


Background:

I have created a class library for .net core (targetting v2.2), and I have a .net core application as well (targetting v2.2).

I am trying to export the library as nuget package and install it in my application. Here is the dependencies for my library

MyLib dependencies

I am able to export it as nuget package and for now I am storing it in local nuget repo. But when I try to install this library package in my application it's not getting installed due to package version conflict for Microsoft.Extensions.Logging. Here's package manager console output.

PackageManagerConsoleOutput

Issue:

I have specified the exact version for Microsoft.Extensions.Logging i.e. [2.2.0] as we could confirm that in the screenshot showing dependency for my library, then why it's getting resolved to version 3.0.0? How could I resolve this issue?

Details about the environment:

  • NuGet product used (Package Manager Console): Package Manager Console Host Version 5.3.1.6268
  • VS version (if appropriate): Microsoft Visual Studio Community 2019 Version 16.3.8
  • OS version (i.e. win10 v1607 (14393.321)): Windows 10 Enterprise Version: 1809

Solution

  • How could I resolve this issue?

    To resolve the strange behavior in your side, you should clean the nuget cache before installing that package in your current project.

    (To make sure the cache is cleaned up, I suggest you go %userprofile%\.nuget\packages to check if there exists Com.lib folder within the Packages folder)

    I have specified the exact version for Microsoft.Extensions.Logging i.e. [2.2.0] as we could confirm that in the screenshot showing dependency for my library, then why it's getting resolved to version 3.0.0?

    I think the one(Com.Mylib) you want to install is not the first one you pack. I mean that you may actually build and pack several different Com.Mylib package with different content. And all of their names is Com.lib.1.0.0.nupkg.

    Nuget stores all nuget cache in %userprofile%\.nuget\packages. So if I once install one PackageA in any project. The cache of PackageA is stored there. And if I open a new project trying to install the PackageA with same version(1.0.0), it actually installs the one from cache. So we will meet this strange behavior:

    enter image description here

    Nuget can recognize the Com.lib package depends on other 2.2.0 packages. But when it tries to install that package, it finds the package has existed in cache. Then he tries to install the one from cache, and I guess content of the one in cache is different from your latest one, then the issue occurs.

    Suggestions:

    1.When developing locally, use project reference instead of Nuget packages.

    2.If you need to test the nuget package every time after packing, please make sure the package version has increased.(1.0.0=>1.0.1=>1.0.3... or beta-1.0.12, preview-xxx)

    3.If you have special reason do use same version 1.0.0, please clean the cache to avoid previous cache affects current project.

    Hope all above helps :)