如何在多个 c# 项目中强制执行相同的 nuget 包版本?

How to enforce same nuget package version across multiple c# projects?(如何在多个 c# 项目中强制执行相同的 nuget 包版本?)
本文介绍了如何在多个 c# 项目中强制执行相同的 nuget 包版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆使用几个 NuGet 包的小型 C# 项目.我希望能够自动更新给定包的版本.不仅如此:如果项目使用与其他项目不同的版本,我会收到警告.

I have a bunch of small C# projects which use a couple of NuGet packages. I'd like to be able to update version of a given package automatically. More then that: I'd like to be warned if a project uses different version from the others.

如何在多个 C# 项目中强制执行相同的版本依赖关系?

How do I enforce same version dependency across multiple C# projects?

推荐答案

我相信我已经找到了解决这个(和许多其他)问题的设置.

I believe I have found a setup which solves this (and many other) problem(s).

我刚刚意识到可以将文件夹用作 nuget 源.这是我所做的:

I just realized one can use a folder as nuget source. Here is what I did:

root
  + localnuget
      + Newtonsoft.Json.6.0.1.nupkg
  + nuget.config
  + packages
      + Newtonsoft.Json.6.0.1
  + src
      + project1

nuget.config 如下所示:

nuget.config looks like this:

<configuration>
  <config>
    <add key="repositoryPath" value="packages" />
  </config>
  <packageSources>
    <add key="local source" value="localnuget">
  </packageSources>
</configuration>

您可以将 Nuget 服务器添加到 nuget.config 以在开发期间访问更新或新的依赖项:

You can add Nuget server to nuget.config to get access to updates or new dependencies during development time:

<add key="nuget.org" value="https://www.nuget.org/api/v2/" /> 

完成后,您可以将 .nupkg 从缓存复制到 localnuget 文件夹来签入.

Once you're done, you can copy .nupkg from cache to localnuget folder to check it in.

我喜欢这个设置有 3 点:

There are 3 things I LOVE about this setup:

  1. 我现在可以使用 Nuget 功能,例如添加道具和目标.如果您有代码生成器(例如 protobuf 或 thrift),这将变得无价.

  1. I'm now able to use Nuget features, such as adding props and targets. If you have a code generator (e.g. protobuf or thrift) this becomes pricesless.

它(部分)解决了 Visual Studio 不复制的问题 所有 DLLs,因为您需要在 .nuspec 文件中指定依赖项,并且 nuget 会自动加载间接依赖项.

It (partially) solves the problem of Visual Studio not copying all DLLs, because you need to specify dependencies in .nuspec file and nuget loads indirect dependencies automatically.

我曾经为所有项目提供一个解决方案文件,因此更新 nuget 包更容易.我还没有尝试过,但我想我也解决了这个问题.我可以为要从给定解决方案导出的项目提供 nuget 包.

I used to have a single solution file for all projects so updating nuget packages was easier. I haven't tried yet but I think I solved that problem too. I can have nuget packages for the project I want to export from a given solution.

这篇关于如何在多个 c# 项目中强制执行相同的 nuget 包版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)