使用相同的nuget包的两个不同版本 [英] Using two different versions of same the nuget package
问题描述
我想使用两个不同版本的同一库(OpenCVSharp 2.x和OpenCVSharp 3.x) 好吧,我将这两个程序包都下载到了单独的项目中(我们称之为OCV2Wrapper和OCV3Wrapper),并在我的项目中引用了这两个包装器.我必须从一个软件包(2.x)重命名库并手动引用它们,因为:
I wanted to use two different version same library (OpenCVSharp 2.x and OpenCVSharp 3.x) Well i downloaded those two packages both to the separate project (lets call it OCV2Wrapper and OCV3Wrapper) and reference both wrappers in my project. I had to renamed libraries from one package (2.x) and reference them manualy because: Can we add 2 different versions of same package in NuGet. I read about external aliases and I used external alias in one of the wrappers (2.x in my case). But I have some major problems:
- 我重命名的库不会复制到启动项目的版本(引用两个包装器的那个库),而是在2.x包装器的构建器中
- 它不起作用,因为它说即使我从2.x包装器手动复制了已重命名的库,也无法从2.x包装器中找到类型.
在C#中针对这种情况的正确方法是什么?
What is the correct approach for this scenario in C#?
我想在解决方案中同时使用两个包装器,因为2.x版本包含算法(SIFT和SURF),而3.x版本包含算法(Kaze和AKaze). 我可以相信这两个软件包都不会使用nuget,但我更喜欢3.x来自nuget并手动配置了2.x版本.
I want to use both wrappers in solution because 2.x version contains algorithms (SIFT and SURF) and 3.x verison contains algorithms (Kaze and AKaze). I can live that both packages would be out of nuget but i prefer that 3.x is from nuget and 2.x version is manually configured.
推荐答案
如上所述,只要引用在两个不同版本的NuGet程序包中进行引用,就不会有问题.
As already stated there is nothing wrong with referencing 2 different versions of of a NuGet package, as long as it's in different Visual Studio Projects that those references are made.
但这也是容易的部分结束的地方,但是我认为还有一些选择.根据您的需求,我会看到以下选项.
But this is also where the easy part ends, but I think there are a few options left. Depending on your needs I see the following options.
- 创建一个后构建步骤,以将多版本程序集注册到GAC中.只要每个程序集具有不同的程序集版本,CLR就会在需要时从GAC中选择正确的程序集.
- 创建一个构建后步骤,将不同的程序集复制到应用程序bin文件夹的子文件夹中,例如 bin/package-v1 和 bin/package-v2 .然后,您可以在应用程序中覆盖 AssemblyResolve 事件,如此处 https://msdn.microsoft.com/zh- us/library/4191fzwb(v = vs.110).aspx
- Create a post build step which register the multi-versioned assemblies into the GAC. As long as each assembly have different assembly version, the CLR will pick up the right assembly from the GAC when needed.
- Create a post build step which copy the different assemblies into a subfolder of you application bin folder like bin/package-v1 and bin/package-v2. Then you can in your application override the AssemblyResolve event like described here https://msdn.microsoft.com/en-us/library/ff527268(v=vs.110).aspx. This will make it possible for you to load the assembly in the right version at the time of need.
- If you don't want to play around with AssemblyResolve, then you can also modify your web/app.config to do assembly redirect/probing as described here https://msdn.microsoft.com/en-us/library/4191fzwb(v=vs.110).aspx
希望这会有所帮助,因此您下次不必修改第三方源代码.
Hope this helps a bit, so you don't have to modify third party source code next time.
这篇关于使用相同的nuget包的两个不同版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!