简体   繁体   English

引用从也使用其中一些包的项目中使用NuGet包的程序集

[英]Referencing assemblies that use NuGet packages from a project that also uses some of these packages

I have a complex library A that consists of several c# projects producing the following assemblies: 我有一个复杂的库A ,它包含几个生成以下程序集的c#项目:

  • A.Core 核心
  • A.Caching A.Caching
  • A.Logging A.Logging
  • A.MongoDb A.MongoDb

The library is written by me, and I have full access to its source code. 该库是由我编写的,我可以完全访问其源代码。 It depends on several NuGet packages, let's say they are: 这取决于几个NuGet包,假设它们是:

  • mongocsharpdriver mongocsharpdriver
  • EnterpriseLibrary.Caching EnterpriseLibrary.Caching
  • EnterpriseLibrary.Logging EnterpriseLibrary.Logging

These libraries (MongoDb driver and EntLib) are not installed in GAC. 这些库(MongoDb驱动程序和EntLib)未安装在GAC中。

I also have a web project B and I want to use library A in it as a reference to a DLL (not including projects into solution B ). 我还有一个Web项目B ,我想在其中使用库A作为DLL的引用(不包括项目到解决方案B )。 The web project depends on several NuGet packages, among them: Web项目依赖于几个NuGet包,其中包括:

  • mongocsharpdriver mongocsharpdriver
  • EnterpriseLibrary.Logging EnterpriseLibrary.Logging

Versions of these packages in solutions A and B do not match as they are not developed synchronously. 解决方案AB中这些软件包的版本不匹配,因为它们不是同步开发的。 Library A was developed a year ago and used as is since then. 图书馆A是在一年前开发的,从那时起就一直使用。

Question: what is the proper way to distribute library A so that: 问题:分发库A的正确方法是什么,以便:

  1. Any solution could use it even if lib A dependencies are not referenced. 即使没有引用lib A依赖项,任何解决方案都可以使用它。
  2. If a solution has its own versions of lib A dependencies, they do not conflict. 如果解决方案具有自己的lib A依赖项版本,则它们不会发生冲突。

My current approach: 我目前的做法:

  1. From my solutions, I reference the four dlls: A.Core, A.Caching, A.Logging, A.MongoDb . 从我的解决方案中,我引用了四个dll: A.Core, A.Caching, A.Logging, A.MongoDb
  2. Then I add all its NuGet dependecies to the solution, if they are not present. 然后,如果它们不存在,我将其所有NuGet依赖项添加到解决方案中。
  3. It breaks sometimes as solution B uses mongocsharpdriver version 1.5 and lib A uses version 1.1 (the versions are not that compatible). 它有时会破解,因为解决方案B使用mongocsharpdriver 1.5版,而lib A使用1.1版(版本不兼容)。

So, what shall I do? 那么,我该怎么办? What will be the correct way of dealing with conflict 'references vs. references of references'? 处理冲突引用与引用引用的正确方法是什么?

The best way to manage shared references is to create your own NuGet package for library A which, among other things, defines any dependencies (including versions) that library A relies on. 管理共享引用的最佳方法是为库A 创建自己的NuGet包 ,其中包括库A依赖的任何依赖项(包括版本)。 If you don't want to publish library A to the public NuGet feed, you can host your own feed . 如果您不想将库A发布到公共NuGet供稿,则可以托管自己的供稿

NuGet has some complex dependency resolution rules that will attempt to locate a dependency version that is compatible with your project and its dependencies. NuGet有一些复杂的依赖项解析规则 ,它们将尝试查找与您的项目及其依赖项兼容的依赖项版本。 Unfortunately, this still won't solve your problem in point 3 above, where your solution and its dependent library use mutually incompatible versions of the same external dependency. 不幸的是,这仍然无法解决您在上面第3点中的问题,其中您的解决方案及其依赖库使用相同外部依赖项的互不兼容的版本。 In this case, you'll just have to re-work either your solution B or library A so that they use the same version of mongocsharpdriver. 在这种情况下,您只需重新使用解决方案B或库A,以便它们使用相同版本的mongocsharpdriver。

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

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