Click here to Skip to main content
Licence 
First Posted 18 Apr 2007
Views 13,689
Downloads 96
Bookmarked 12 times

DLL Hell Solution in DOTNET

By | 7 May 2007 | Article
This article provides an example how to execute two DLLs with same name but different version number simultaneously

Introduction

In dotnet two or more assemblies with the same name but with different version number can exist in the GAC(Global Assembly Cache). This is the solution of DLL Hell which exists in component programming previously. I am going to provide one example for this.

1. How to create a strong named assembly with different version numbers then installing them to GAC.

2. Using Reflection how to use them in client application with different version.

Background

Elementary Knowledge of reflection is required.

Using the code

Creating DLL with version 1.0.0.0:

In Visual Studio

File -> New -> Project -> visual c# project ->Class Library.

Provide Name (I have provided MYCOM) and suitable location. Press OK button.

Change the name of class1 as Person and add one method Display.

using System; 
namespace MYCOM 
{ 
    public class Person 
    { 
        public Person() 
        { 
        } 
        public string Display() 
        { 
            return "I am person with Version 1.0.0.0"; 
        } 
    } 
}

From Visual Studio Command Prompt type > sn –k mykey.snk

It will generate key file. Copy the file in the same folder where DLL will be generated (project folder\bin\debug)

Now in AssemblyInfo.cs file enter

[assembly: AssemblyVersion("1.0.0.0")]

[assembly: AssemblyKeyFile("mykey.snk")]

Now build the project.

Strong named assembly MYCOM.dll will get generated in debug folder.

From Visual studio command prompt type > gacutil –i MYCOM.dll

MYCOM.dll gets installed in GAC.

Now change the Display Funtion as

public string Display()
{
    return "I am person with Version 1.0.0.1";
}

Change in Assembly.cs file

[assembly: AssemblyVersion("1.0.0.1")]

Build the project MYCOM.dll with Version number 1.0.0.1 will get generated in bin\debug folder.

From Visual studio command prompt type > gacutil –i MYCOM.dll

MYCOM.dll gets installed in GAC.

Now GAC contains two MYCOM.dll but with different version.

Client Application

In Visual Studio

File -> New -> Project -> visual c# project ->Console Application

Press OK.

Now type the following lines


using System;
using System.Reflection;
using System.Globalization;
using System.Configuration;
namespace ConsoleApplication1
{
    class Class1
    {
        public static void Main(string[] args)
        {
            Assembly objm = Assembly.Load("MYCOM, Version=1.0.0.0, 
                Culture=neutral, PublicKeyToken = 5b5aaf21705d336b");
            Type objmycom = objm.GetType("MYCOM.Person",true,true);
            BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public 
                | BindingFlags.Static | BindingFlags.Instance | 
                BindingFlags.DeclaredOnly);
            MethodInfo m = objmycom.GetMethod("Display",flags);
            object myobj = Activator.CreateInstance(objmycom,false);
            object result = m.Invoke(myobj,BindingFlags.InvokeMethod, null,
                null, CultureInfo.CurrentCulture);
            Console.WriteLine("result.ToString());
                Console.Read();
        }
    }
}

You can get PublicKeyToken from GAC.

From Visual Studio command Prompt type >mscorcfg.msc

Click on view list of assemblies in the assembly cache. Right click mouse on your assembly (MYCOM) select correct version 1.0.0.0à select Properties and the copy the public key token from there and paste in your assembly name.

Assembly objm = Assembly.Load("MYCOM, Version=1.0.0.0, Culture=neutral, PublicKeyToken = 5b5aaf21705d336b");

Now execute you will see the output

"I am person with Version 1.0.0.0";

And if you change the Version as 1.0.0.1 then output will be

"I am person with Version 1.0.0.1";

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

sarvesh.upadhyay

Web Developer

India India

Member

Sarvesh Upadhyay is Software developer on various domain like CRM, CMS, Inventory Management System, Telecome Billing, E-Commerce application etc. He has worked on Asp.Net, C#,Javascript, MS-SQL Server,Web Services,.Net Remoting, MSMQ,COM Programming in VB 6.0. He has worked on Saleslogix CRM. Currently he is working in Birlasoft India Ltd. as Senior Software Engineer.
He has done B.Sc.(Hons) from Calcutta University and MCPD in dotnet 2.0 Enterprise Application Developer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTo be a member in dotnet developer team PinmemberRevathy rani20:51 4 Jun '11  
Questionregarding to dll hell Pinmembercreative2002guy2:45 11 Dec '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 8 May 2007
Article Copyright 2007 by sarvesh.upadhyay
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid