简体   繁体   English

在同一个Project.Net Core中执行不同的Nuget Package版本

[英]Enforce Different Nuget Package Version in the same Project .Net Core

I am working on API versioning on.Net Core 3.1.我正在处理.Net Core 3.1 上的 API 版本控制。 The issue is that we are consuming an external nuget package that will apply different versions on different API versions.问题是我们正在使用外部 nuget package ,它将在不同的 API 版本上应用不同的版本。 For example, for API Version 1.0, I need to use V2.0 of this nuget package;例如对于API 1.0版本,我需要使用这个nuget package的V2.0; for API Version 2.0, I need to use V3.0 of this nuget package.对于 API 版本 2.0,我需要使用此 nuget package 的 V3.0。

So if I use the strategy this way: https://dev.to/99darshan/restful-web-api-versioning-with-asp-net-core-1e8g by spliting the controller classes in the same project, then it would force all of my API versions with the same nuget package versions.因此,如果我以这种方式使用该策略: https://dev.to/99darshan/restful-web-api-versioning-with-asp-net-core-1e8g通过在同一项目中拆分 controller 类,那么它将强制我所有的 API 版本都具有相同的 nuget package 版本。

I was wondering what's the better way to do API versioning in my scenario?我想知道在我的场景中进行 API 版本控制的更好方法是什么?

As long the features you are using are functionally compatible (typically by source code), then the NuGet package versions do not need to match.只要您使用的功能在功能上兼容(通常通过源代码),则 NuGet package 版本不需要匹配。 You did not specifically say, but I get the impression you are defining API versions by applying attributes and that is the only surface area used by the code and libraries that define the corresponding controllers.您没有具体说,但我得到的印象是您通过应用属性来定义 API 版本,这是定义相应控制器的代码和库使用的唯一表面区域。

You're describing a typical setup for plug-in or composition based API applications.您正在描述基于插件或组合的 API 应用程序的典型设置。 Assuming that is correct, then the NuGet package version is only interesting to the host application (eg the hosting server code).假设这是正确的,那么 NuGet package 版本对主机应用程序(例如托管服务器代码)感兴趣。 The host application that runs the server must use a version of the NuGet package that is greater than or equal to the highest version used by any of the other referenced assemblies.运行服务器的主机应用程序必须使用大于或等于任何其他引用程序集使用的最高版本的 NuGet package 版本。 The rest is handled by .NET, not NuGet. rest 由 .NET 处理,而不是 NuGet。 Modern .NET projects enable automatic binding redirects;现代 .NET 项目支持自动绑定重定向; typically without any developer configuration.通常没有任何开发人员配置。 This means that if your controller libraries are an array of say version 1.0 , 2.0 , and 3.0 and the host references 3.0 , then all libraries ultimately use 3.0 at runtime.这意味着,如果您的 controller 库是1.02.03.0版本的数组并且主机引用3.0 ,那么所有库最终在运行时都使用3.0 The only caveat to this is that all libraries must target the same runtime.唯一需要注意的是,所有库都必须针对相同的运行时。 For example, you cannot make a mix of full .NET Framework and .NET 5.0 (formerly .NET Core) work together.例如,您不能将完整的 .NET 框架和 .NET 5.0(以前的 .NET 核心)混合在一起工作。 As long as the runtimes are aligned, you should not have any problem getting everything aligned;只要运行时是对齐的,那么对齐所有内容应该没有任何问题; regardless, of NuGet package or assembly version.不管是 NuGet package 还是组装版本。

There are also, at least, two other solutions to this problem.对于这个问题,至少还有另外两种解决方案。

  1. Use Conventions - API Versioning provides a fluent interface for defining API versions by convention .使用约定- API 版本控制提供了一个流畅的接口,用于按约定定义 API 版本。 It was designed to solve the exact problems of applying API version metadata to controllers defined in another library.它旨在解决将 API 版本元数据应用于另一个库中定义的控制器的确切问题。 This configuration would likely be defined in the host application.此配置可能会在主机应用程序中定义。 You can also define your conventions so you don't have to define every version individually.您还可以定义您的约定,这样您就不必单独定义每个版本。 The VersionByNamespaceConvention uses this approach to derive a single API version by the containing .NET namespace. VersionByNamespaceConvention使用此方法通过包含 .NET 命名空间派生单个 API 版本。
  2. Use Custom Metadata - Contrary to popular belief, API Versioning does not care about attributes.使用自定义元数据- 与流行的看法相反,API 版本控制不关心属性。 It provides some attributes as a simple, out-of-the-box way to apply metadata, but it's not a hard requirement.它提供了一些属性,作为一种简单的、开箱即用的方式来应用元数据,但这不是硬性要求。 It only cares about IApiVersionProvider .它只关心IApiVersionProvider This means that you can easily roll your own attributes and then plug it into API Versioning by implementing IApiVersionProvider or IControllerConvention .这意味着您可以轻松滚动自己的属性,然后通过实现IApiVersionProviderIControllerConvention将其插入 API 版本控制。 This would allow you to create metadata library, with .NET Standard if necessary, that will likely never change and you can subsequently add to the versioning configuration.这将允许您创建元数据库,如有必要,使用 .NET 标准,这可能永远不会改变,您可以随后添加到版本控制配置。

#2 is a more advanced scenario. #2 是一个更高级的场景。 I'm not aware of any existing examples that would demonstrate all of that end-to-end, but if you choose that path and have questions, I can push you in the right direction.我不知道有任何现有的例子可以证明所有这些端到端,但如果你选择了这条路并且有问题,我可以把你推向正确的方向。

暂无
暂无

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

相关问题 Nuget package 版本冲突 .net 内核虽然版本相同 - Nuget package version conflict .net core though having same version Nuget 包内容文件未复制到 .NET Core 项目 - Nuget package contentFiles not copied to .NET Core project 如何在多个 c# 项目中强制执行相同的 nuget 包版本? - How to enforce same nuget package version across multiple c# projects? 获取包含在 csproj in.Net Core 3.1 中的 nuget package 版本 - Get nuget package version included in csproj in .Net Core 3.1 将 C# 文件复制到 nuget ZEFE90A8E604A7C840E88D03A67F6D7 上的 .NET 核心项目中 - Copy C# files into a .NET Core project on nuget package installation 将.NET Core项目发布到Nuget - 要使用哪个框架版本? - Publishing a .NET Core project to Nuget - which framework version to use? 具有相同nuget包的项目引用了不同版本的程序集 - Projects with same nuget package referencing different version of assembly 为什么在.Net 4.7.2项目中引用.Net标准nuget包会导入很多.Net核心库? - Why referencing a .Net standard nuget package in a .Net 4.7.2 project import a lot of .Net core lib? 为什么.net对于相同的nuget包显示版本冲突,而该nuget包是第三方库/包的内部依赖项? - Why .net shows version conflict for same nuget package which is internal dependency for 3rd party library/package? 对两个 nuget 包 C# .net 框架使用不同版本的包 - Use Different version of package for two nuget package C# .net framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM