[英]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.0
、 2.0
和3.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.对于这个问题,至少还有另外两种解决方案。
VersionByNamespaceConvention
uses this approach to derive a single API version by the containing .NET namespace. VersionByNamespaceConvention
使用此方法通过包含 .NET 命名空间派生单个 API 版本。IApiVersionProvider
.IApiVersionProvider
。 This means that you can easily roll your own attributes and then plug it into API Versioning by implementing IApiVersionProvider
or IControllerConvention
.IApiVersionProvider
或IControllerConvention
将其插入 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. #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.