本文介绍了NuGet 跨项目依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于使用 NuGet 包共享代码的生产工作流程.

My question is about production workflow with NuGet packages for shared code.

假设我们有项目 Pr1,它依赖于 NuGet 包 N1,依赖于包 N2.添加 Pr1 作为解决方案中其他项目的参考项目.这些项目还依赖于包 N1 和 N2.

Say we have project Pr1, that depends on NuGet package N1, that depends on package N2. Pr1 added as a reference projects for other projects within the solution.Those projects also have dependencies on packages N1 and N2.

现在想象一下,我对 N2 包进行了更改.之后,我需要为 N1、Pr1 以及同时使用 N2 和 Pr1 的每个项目更新 N2 的包版本,因为如果我不这样做,我可能会遇到运行时错误.它们的原因实际上是程序集被复制到依赖项目的 bin 目录,并且该版本与它构建的版本不同.

Now imagine, I've made changes in N2 package.After that I need to update package version of N2 for N1, for Pr1 and also for every project that use N2 and Pr1 together, because if I don't do that, I can get runtime errors. The cause of them is in fact that assemblies is being copied to bin directory of dependent projects, and that version is not the same it was build against.

这个工作流程比 SVN 外部形式的共享代码更痛苦,但我们正在迁移到 Git,并且共享代码不是它可以有效工作的东西(子树和子模块与外部不同!).

This workflow give a pain even greater than shared code in form of SVN-externals, but we are migrating to Git, and shared code is not the thing it can work effectively (subtrees and submodules is no the same as externals!).

此外,我们不仅有一个项目解决方案,而且有很多,而且依赖链比这个玩具示例要长得多,因此更改版本变得非常痛苦并且浪费时间.

Also, we have not only one solution of projects, but many, and dependency chain is much longer than for this toy example, so changing versions become very painful and give a filing of wasting of time.

推荐答案

我建议将您的项目移动到 VS 2017 的 PackageReference 样式(远离 packages.config)并且现在引入了 NuGet 4(随着最近的 15.2 更新,这也支持经典 .net 项目——不仅是 .net core/.net 标准项目).最大的区别是依赖关系是传递性的,所以你只需要指定一个从 Pr1 到 N1 的包引用,并自动获得 N1 引用的 N2 版本.如果您随后将项目 Pr2 中的项目引用添加到 Pr1,该项目将自动获取对 N1 和 N2 的引用.因此,如果您更新 Pr1 使用的 N1 版本,它将获得 N1 所需的 N2 更新,而 Pr2 也将使用新的 N1 和 N2,而不会进行任何更改.

I suggest moving your projects to the PackageReference style (away from packages.config) that VS 2017 and NuGet 4 now introduced (with the recent 15.2 update, this is supported for classic .net projects as well - not only .net core / .net standard projects). The big difference is that dependencies flow transitively, so you only specify a package reference from Pr1 to N1 and automatically get the version of N2 that N1 references. If you then add a project reference from a project Pr2 to Pr1, this project will get references to N1 and N2 automatically. So if you update the version of N1 that Pr1 uses, it will get the updates to N2 that N1 requires and Pr2 will also use the new N1 and N2 without making any changes.

请参阅这篇 NuGet 博文关于如何使用PackageReference,特别是其他非.NET Core的项目类型呢?"部分.

See this NuGet blog post for how to use PackageReference, especially the section "What about other project types that are not .NET Core?"

这篇关于NuGet 跨项目依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 18:40