English 中文(简体)
Reference two equal assemblies, only public keys differ
原标题:

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:

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, 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.

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.

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

UPDATE

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.

问题回答

I wonder if AssemblyResolve would work:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name == "full name with old key")
    {
        return typeof(SomeTypeInReferencedAssembly).Assembly;
    }
    return null;
}

He re I m assuming that you ve referenced your preferred version and can use the type SomeTypeInReferencedAssembly to get the Assembly; you could also use:

return Assembly.Load("full name with new key");

I haven t tried it, but here I m essentially saying "deploy one version of the dll, and when the system asks for the old one - lie".

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.





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签