Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

c# - Need a way to reference 2 different versions of the same 3rd party DLL

I have a solution with 2 projects: Proj1 and Proj2, where Proj1 is the startup project.

Proj1 references Proj2 (in order to call Proj2's class) and it has existing code that also references a 3rd party DLL called A, versioned at 1.0.0.0.

Proj2 references the same 3rd party DLL A, but it references it at version 2.0.0.0, since the class in this project needs newer implemenation that was not available in 1.0.0.0.

So far I've tried the following: 1. Switched "Specific Version" to true when referencing A 2. Added a folder called "v2Folder" in Proj2 and added A v2.0.0.0 to it, set its "Copy to Output Directory" to Copy Always 3. Added "probing path" to app.config to point to the sub-folder with the v2.0.0.0 DLL

What I want is to see A v1.0.0.0 in the normal in folder, and A v2.0.0.0 in inv2Folder, and I expect that when I run my Proj1.exe, Proj1's old code will still call A v1.0.0.0's methods, and only call A v2.0.0.0's methods when calling what is implemented by Proj2.

The problem is, when I build my solution, v1.0.0.0 got replaced by v2.0.0.0, the build log has something like "No way to resolve conflict between "A, Version=2.0.0.0, Culture=neutral, PublicKeyToken=blah" and "A, Version=1.0.0.0, Culture=neutral, PublicKeyToken=blah". Choosing "A, Version=2.0.0.0, Culture=neutral, PublicKeyToken=blah" arbitrarily.".

Can someone help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's achievable even if the two dll versions have the same public token.

Here the steps to achieve this:

  • Ensure that the two version of the dll will be copied to the target directory
    • Add the two dll's as content items of the project
    • Enable local copy for both
  • Ensure that the two version of the dll will be referenced at compile time
    • Add the two dll's in the project as references
    • Disable local copy for both

Just add the references is not enough, since only the newer one will be copied (even if you enable local copy for both). This give a project tree like this:

Project tree

  • Ensure that the two version of the dll can be distinguished at compile time
    • Add an alias for at least on of the references

Aliases field position

  • Reference the libraries in the code using extern alias (see @drf response)

Example code

At this point you can compile but you still have problems at run-time. To fix those:

Auto-generate binding redirects checkbox position

  • Edit app.config to add an assemblyBinding.
    • assemblyIdentity is the concerned dll.
    • bindingRedirect map a version range (oldVersion) to a fixed version (newVersion).
    • codeBase map a fixed version to a file path (href).

bindingRedirect newVersion and codeBase version must match and match the version of the used dll.

Here, it's all about the dll assembly version, not the file version app.config example

Here is the program output:

Console output

This hack source code is available here.

Edit: As sharpiro commented, there is still a warning when the project is built, this is related to this msbuild bug for witch this answer is a workaround.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...