C# 2.0: Using different versions of the same dll in one application
Lot of things have become really easy to do in C#2.0. I needed to load 2 versions of the same class library dll in my application. The format of output file had changed between versions. So I need to deserialize an xml using the older version and then convert it to the newer version and then serialize it out. So at the same time I needed to load both the old and new dlls. Lets assume that you have the following implementation of the class libraries
Older version
using System;namespace MyLibrary{ public class MyClass { public static string method() { return "From old version ClassLibrary2"; } }}
Newer version
using System;namespace MyLibrary{ public class MyClass { public static string method() { return "From new version ClassLibrary2"; } }}
The structure of both libraries are exactly the same and they have the same namespace and classes and only differ in implementation. If I add reference to both the class library dlls and try to access the methods in the client using the following code
using System;using System.Text;namespace ClientApp{ class Program { static void Main(string[] args) { Console.WriteLine(MyLibrary.MyClass.method()); } }}
Compilation will fail with the error "The type 'MyLibrary.MyClass' exists in both 'Somepath\ClassLibrary.dll' and 'AnotherPath\ClassLibrary.dll'". The reason being that the access becomes ambiguous.
This is where the new namespace alias qualifier and the extern alias comes into the picture. First we write the client application (program.cs) as
externalias oldVer; externalias newVer; using System;using System.Text;namespace ClientApp{ class Program { static void Main(string[] args) { Console.WriteLine(oldVer:: MyLibrary.MyClass.method()); Console.WriteLine(newVer:: MyLibrary.MyClass.method()); } }}
In the code-snippet above all the important bits are marked in bold. The extern alias declaration creates a namespace parallel to the global namespace. Pre C#2.0 all namespaces were rooted at the global namespace, which is no more the case. We are telling the compiler that two other namespace oldVer and newVer exists which are externally defined. We now use the :: operator to access inside the namespace alias. We could have used the regular . qualifier but the :: qualifier ensures that the identifier on the left-hand of the :: qualifier is an extern or using alias.
The next step is to define the external alias. We can do it using the command line as follows
csc /r:oldVer= Somepath\ClassLibrary.dll /r:newVer= AnotherPath\ClassLibrary.dll program.cs
So the contents of the older version is loaded under the oldVer alias and the new version under newVer. So all access ambiguity is removed.
The same thing can also be acheived using the VS IDE. Add the reference to both the dlls in your client application solution. Then in the Solution Explorer under the reference node select the first (old version) class library. In the property window change Aliases field from global to oldVer. Make similar change after selecting the newer class library as well. You are then good to go....
Comments
Anonymous
May 15, 2006
Hey,
Thanks very much for this. This is what I was precisely searching for.Anonymous
July 20, 2006
Is there any way we can achieve the functionality provided by alias in c# 2.0 using the earlier version of c# (1.2)?
Thanks,
VenuAnonymous
July 20, 2006
Nope. You'll need to wrap up the dlls in some other code and then callAnonymous
January 07, 2007
Good one. Thanks for informationAnonymous
February 22, 2007
Thanks Man,Exactly what I was looking for...Anonymous
December 06, 2007
its really good.i was faced the same problem when i am trying to 9.1 & 10.0 version of same assembly in my application.Anonymous
June 24, 2008
The comment has been removedAnonymous
August 06, 2008
i want to use the same concept for windows mobile is there any way to give reference for the dlls? regards kiranAnonymous
September 02, 2008
Maintenance: It seems that assemblies must be signed.Anonymous
September 11, 2008
Hi This is the exact kinda solution I was looking for but the problem I have is how would i add the two dlls reference when they both have the same name. In my case I cannot rename one of the dlls. They both need to have the same name but are different versionsAnonymous
September 11, 2008
Aimtesh why do you have to rename? Just use the same named dll from two different folders. Obviously two dlls with the same name cannot be in the same folder...Anonymous
September 13, 2008
The comment has been removedAnonymous
September 13, 2008
Are the two dlls strong named?Anonymous
September 14, 2008
Yes both the dlls have strong name but still does not work. Both the dlls are in separate folders but when u try and add reference to the dlls via the VS IDE, it produces an errorAnonymous
December 26, 2008
Is there a solution to this problem then - VS IDE wont allow adding the two dlls with different versions?Anonymous
January 02, 2009
PingBack from http://blueonionsoftware.com/blog.aspx?p=f0fc19c1-bb4f-4985-adcf-a9fec3a754a1Anonymous
June 02, 2009
Perfect! The thing I was looking for. I need to write a software that runs on AutoCAD 2008 and 2010. They changed the DLLs but with the same naming structure and I had a hard time to get my app running on both without create two versions of my software. You had the solution. I hope it runs as expected (the little test app I made worked fine).Anonymous
March 31, 2011
Thanks! You just saved me a s***load of time.Anonymous
August 23, 2011
hello I have to Major applications and one small and both major application is using the small one with diff version of dlls . so i am not able to run both the major application simultaneously on my system . Even after running one when i run the second all dlls are overwrited . What should i do to run both the application simultaneouslyAnonymous
November 10, 2011
Hi Thanks for the useful post. Please let me know how I can add these alias references in xaml code in wpf. ThanksAnonymous
May 28, 2012
i got en error when i refer the second dll with the same name but different versions. can you please provide sample application using windows forms.Anonymous
September 14, 2012
Thx ! Exactly what we need for a converter (old <==> new versions :) )Anonymous
October 02, 2012
I have the same problem as other that have responded. I get an error when I try to include different versions of the same dll with the same name. Is there a way around that. Otherwise, how can this solution be viable?Anonymous
October 09, 2012
Well, this post is strange, the same question raised by so many people, yet no answer, while so many pros...Anonymous
September 12, 2013
Thank you very much !!. This is what, I am looking for...Anonymous
December 04, 2013
Good article! And, Could you explain how to use "extern alias" in the web project (asp.net)?Anonymous
February 17, 2014
To add two assemblies with same name I've edited project file manually: <Reference Include="File - New"> <HintPath>FolderAFile.dll</HintPath> <Aliases>new</Aliases> </Reference> <Reference Include="File - Old"> <HintPath>FolderBFile.dll</HintPath> <Aliases>old</Aliases> </Reference>Anonymous
March 18, 2014
To make this last example work, we also had to use AssemblyResolve to load the correct dll from file, otherwise it would not know how to load the correct assembly from the executing directory. To make this work, we copied two dll files into the executing directory and renamed each with an appropriate version number to differentiate the two dlls.Anonymous
March 18, 2014
steveb is correct, I answered this at StackOverflow, credit goes to him. stackoverflow.com/.../22486097Anonymous
April 03, 2014
thank you for this article , i have th esame problem , but I Wonder where excecute csc /r:oldVer=SomepathClassLibrary.dll /r:newVer=AnotherPathClassLibrary.dll program"Anonymous
April 14, 2014
hi m using itextcsharp dll and there is not alias name given for itAnonymous
July 29, 2015
Hai... When i try to add two dll with different alias name as oldver and newver, adding refernces is not working for second dll. There is name conflicts.Can anyone please help me to recover from the above problem please guys. Thanks in advance...Anonymous
August 05, 2015
The comment has been removed