参考两个相同的组件,只有公钥不同 [英] Reference two equal assemblies, only public keys differ

查看:307
本文介绍了参考两个相同的组件,只有公钥不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Visual Studio 2008的项目中引用两个(外部)组件(A + B),这两种碰巧引用相同的第三个组件(C)。然而,组件A预计组装C到有一个公共密钥这不同于什么程序集B希望它有。

My Visual Studio 2008 project references two (external) assemblies (A+B) both of which happen to be referencing the same third assembly (C). However, assembly A expects assembly C to have a public key which is different from what assembly B expects it to have.

下面是明显异常的一个例子:

Here's an example of the obvious exception:

无法加载文件或程序集Newtonsoft.Json,版本= 3.5.0.0,文化=中性公钥= 9ad232b50c3e6444或它的某一个依赖。找到的程序集清单定义不匹配的程序集引用。 (从HRESULT异常:0x80131040)

Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=9ad232b50c3e6444' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Ofcourse,我不能够把的C两个版本(仅公用密钥不同)在同一个目录中,其文件名是相同的。其次,我发现,使用配置文件中的程序集绑定只允许版本映射,不公开键映射。

Ofcourse, I would not be able to put both versions of C (only differing by public key) in the same directory, as their filenames are equal. Second, I've found that using assembly binding from the configuration file only allows version mapping, not public key mapping.

我也试图把的组件C一根在一个单独的目录,并配置CLR在该目录中加载程序集时进行搜索。我不能得到那个工作很遗憾。

I've also tried to put one of the assemblies C in a separate directory and configure the CLR to search in that directory when loading assemblies. I could not get that to work unfortunately.

我知道,重新编译外部库中的一个(其中一个正好是开源)将解决这个问题,但我不希望这种负担添加到我的维护计划,如果它不是绝对必要的。

I'm aware that recompiling one of the external libraries (one of them happens to be open sourced) would fix this problem but I don't want to add that burden to my maintenance plan if it is not absolutely necessary.

所以我的问题是:如何将我引用组装C,它只能通过公钥不同的两个版本

So my question is: how would I reference both 'versions' of assembly C, which only differ by public key?

更新

我偶然发现了<一href="http://stackoverflow.com/questions/2460542/using-different-versions-of-the-same-assembly-in-the-same-folder/2460626#2460626">this回答以一个相关的问题,提供了使用ilmerge一个有趣的解决方案。我没有检查出来呢,但它可能是有用的人这个问题苦苦挣扎。

I stumbled upon this answer to a related question, providing an interesting solution using ilmerge. I haven't checked it out yet but it may be useful to anyone struggling with this problem.

推荐答案

您应该永远不会有两个组件用的版本相同的,但是的不同的公共密钥的,这是一个食谱灾害。如果实际装配的版本不同,则绝对最简单的解决办法就是将它们放置在的全局程序集缓存(GAC)。这不,但是,起到很好的,如果你要处理与在C中定义和使用类型的实例A和B(例如C声明的MyType 和你选自B获得的的MyType 的实例并将其传递给A.至于运行时而言,这两种类型都完全没有相互的一边共享名称)。

You should never have two assemblies with the same version but different public keys, that's a recipe for disaster. If the actual assembly versions are different, then the absolute easiest solution is to place them in the global assembly cache (GAC). This will not, however, play nicely if you'll be dealing with instances of types that are defined in C and used in both A and B (for example, C declares MyType, and you obtain an instance of MyType from B and pass it to A. As far as the runtime is concerned, these two types have absolutely nothing to do with one another aside from sharing a name).

如果你正在寻找一个临时的解决方案,那么我将与马克的去;这听起来像它应该很好地工作。话虽这么说,但是,事实上,你会沿着这条道路应该是一个巨大的红色标志。

If you're looking for a temporary solution, then I would go with Marc's; it sounds like it should work perfectly. That being said, however, the fact that you're going down this road should be a giant red flag.

这篇关于参考两个相同的组件,只有公钥不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆