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

extern alias oldVer;

extern alias 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 (33)

  1. Anonymous says:

    Hey,

    Thanks very much for this. This is what I was precisely searching for.

  2. Anonymous says:

    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,

    Venu

  3. Nope. You’ll need to wrap up the dlls in some other code and then call

  4. Anonymous says:

    Good one. Thanks for information

  5. Anonymous says:

    Thanks Man,

    Exactly what I was looking for…

  6. ramana reddy says:

    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.

  7. Maintenance says:

    Thanks for the info..

    How do I reference two different versions of the same DLL?

    if I try to add a second version, visual studio throws up an error message saying that a reference to that component already exists in the project 😐

  8. kiran says:

    i want to use the same concept for windows mobile is there any way to give reference for the dlls?

    regards

    kiran

  9. yol says:

    Maintenance: It seems that assemblies must be signed.

  10. Amitesh says:

    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 versions

  11. 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…

  12. Pete says:

    I’m with Amitesh, if I try to add a different version of the same dll (located in a different folder) I get an error from .NET.  "A reference to the component xxxx already exists in the project.".

    Any ideas?

  13. Are the two dlls strong named?

  14. Amitesh says:

    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 error

  15. Darshat says:

    Is there a solution to this problem then – VS IDE wont allow adding the two dlls with different versions?

  16. Armin says:

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

  17. Pah Joker says:

    Thanks!  You just saved me a s***load of time.

  18. Amita says:

    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 simultaneously

  19. Sai says:

    Hi

    Thanks for the useful post.

    Please let me know how I can add these alias references in xaml code in wpf.

    Thanks

  20. vadivel says:

    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.

  21. Dude76 says:

    Thx !

    Exactly what we need for a converter (old <==> new versions 🙂 )

  22. StarRider says:

    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?

  23. Nate says:

    Well, this post is strange, the same question raised by so many people, yet no answer, while so many pros…

  24. Vijay says:

    Thank you very much !!. This is what, I am looking for…

  25. Hao says:

    Good article!

    And, Could you explain how to use "extern alias" in the web project (asp.net)?

  26. Roman says:

    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>

  27. steveb says:

    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.

  28. jordanr says:

    steveb is correct, I answered this at StackOverflow, credit goes to him.

    stackoverflow.com/…/22486097

  29. maitoti says:

    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"

  30. nidhi mishra says:

    hi m using itextcsharp dll and there is not alias name given for it

  31. Samas says:

    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…

  32. Sant.lev says:

    Hi, i'm dealing with this problem right now, i have made this work in most places in my application but i still have some places where i can't get it done. Can anyone provide help? My mail is santiagojlevy at hot mail dot com

    Thanks in advance.

    P.s. : What? My comment is not spam. -.-