简体   繁体   English

多目标NuGet程序包的错误依赖关系解决

[英]Erroneous dependency resolving for multi-targeting NuGet package

I am being confused by how dependencies are resolved when using a group-based dependency specification in NuGet. 我在NuGet中使用基于组的依赖项规范时如何解决依赖项感到困惑。

I have a Visual Studio project targeting .NET Framework 4.6.1, with a NuGet dependency to a NuGet package (internal to my company): 我有一个针对.NET Framework 4.6.1的Visual Studio项目,该项目具有对NuGet包的NuGet依赖关系(在我公司内部):

This is in the packages.config file of my project: 这是在我的项目的packages.config文件中:

<package id="Name.Of.My.Package" version="2.0.65" targetFramework="net461" />

And this is in the .csproj file: 这是在.csproj文件中:

<Reference Include="Name.Of.My.Package, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL"> <HintPath>..\\packages\\Name.Of.My.Package.2.0.65\\lib\\net45\\Name.Of.My.Package.dll</HintPath> </Reference>

That package is a multi-targeting package with a group-based dependency specification, which looks like this when I get it straight from the .nuspec file obtained by opening the packages\\Name.Of.My.Package.nupkg file as an archive: 该程序包是具有基于组的依赖关系规范的多目标程序包,当我直接从通过打开packages\\Name.Of.My.Package.nupkg文件作为存档而获得的.nuspec文件中获取该packages\\Name.Of.My.Package.nupkg时,它看起来像这样:

<dependencies> <group> <dependency id="Newtonsoft.Json" version="11.0.2" /> </group> <group targetFramework=".NETFramework4.0"> <dependency id="Microsoft.Bcl.Async" version="1.0.168" /> </group> </dependencies>

And the packages\\Name.Of.My.Package\\lib folder contains: 然后, packages\\Name.Of.My.Package\\lib文件夹包含:

  • net40 net40
  • net45 net45
  • nestandard2.0 nestandard2.0

So my understanding here is that because my project is in net461, the reference should be added to the net45 version of my package (as seems to be the case if I look at the .csproj file), but more importantly that the only implicit dependency should be to Newtonsoft. 因此,我的理解是,由于我的项目位于net461中,因此应该将引用添加到程序包的net45版本中(就像我查看.csproj文件时的情况一样),但更重要的是,唯一的隐式依赖关系应该去Newtonsoft。

But this happens when I try to remove the Microsoft.Bcl.Async package in the package manager console: 但是,当我尝试在程序包管理器控制台中删除Microsoft.Bcl.Async程序包时,会发生这种情况:

Uninstall-Package Microsoft.Bcl.Async Attempting to gather dependency information for package 'Microsoft.Bcl.Async.1.0.168' with respect to project 'Name.Of.My.Project', targeting '.NETFramework,Version=v4.6.1' Resolving actions to uninstall package 'Microsoft.Bcl.Async.1.0.168' Uninstall-Package : Unable to uninstall 'Microsoft.Bcl.Async.1.0.168' because 'Name.Of.My.Package.2.0.65' depends on it.

This is happening in the latest version of Visual Studio 2017 (15.8.6). 这是最新版本的Visual Studio 2017(15.8.6)中发生的情况。

Erroneous dependency resolving for multi-targeting NuGet package 多目标NuGet程序包的错误依赖关系解决

This is the correct behavior of nuget. 这是nuget的正确行为。 As we know, .NET Frameworks are backwards compatible. 众所周知, .NET Frameworks是向后兼容的。 Meaning if your project is targeting v4.6 , you can consume packages with lower versions, such as v4.5 , v4.0 . 这意味着,如果您的项目面向v4.6 ,则可以使用版本较低的软件包,例如v4.5v4.0

NuGet's specialty is compatibility checking (if packages are authored correctly ofc) :) NuGet knows the available frameworks are v3.5, v4.0, v4.6 and netstandard1.3. NuGet的专长是兼容性检查(如果软件包是由ofc正确编写的):) NuGet知道可用的框架是v3.5,v4.0,v4.6和netstandard1.3。 The "nearest" compatible framework with v4.5 is v4.0, so it'll pick the v4.0 assets when you install it . v4.5的“最近”兼容框架是v4.0,因此在安装时会选择v4.0资产

Source: How to figure out the information of dependency packages for missing frameworks 来源: 如何找出缺少框架的依赖包信息

So nuget will install the dependence "nearest" backwards compatible framework v4.6.1, in you case, the dependency Microsoft.Bcl.Async will also be installed. 因此,nuget将安装依赖项“最近”向后兼容的框架v4.6.1,在这种情况下,还将安装依赖项Microsoft.Bcl.Async

That is the reason why you could not uninstall the package Microsoft.Bcl.Async when you have package Name.Of.My.Package installed. 这就是为什么在安装软件包Name.Of.My.Package时无法卸载软件包Microsoft.Bcl.Async的原因。

For example, when you add the package Microsoft.AspNet.WebApi.Client 5.2.6 to the .net framework 4.6.1 project, nuget will also add the dependency Newtonsoft.Json (>= 6.0.4) under the .net framework 4.5: 例如,当您将包Microsoft.AspNet.WebApi.Client 5.2.6添加到.net Framework 4.6.1项目中时,nuget还将在.net framework 4.5下添加依赖项Newtonsoft.Json (>= 6.0.4)

在此处输入图片说明

在此处输入图片说明

Check this document for some more details. 有关更多详细信息,请查看此文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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