Saturday, January 22, 2011

Side by Side execution of .Net DLL

In my last post I mentioned that I will publish blog on Side by side execution. So here it is.

"Side by side execution is the ability to run multiple versions of same component on the same computer"

Above is the definition of the side by side execution. So how is that possible. I will mention it with code and simple example. But before that let's go through two important terms related to this.

1) Strong named Assembly

Strong name gives unique identity to the assembly.It consists of text name, version information and a public key. You can assign strong name to any assembly using windows SDK or visual studio.Strong name guarantees the name uniqueness for assembly. One assembly is signed with strong name , that will have name that is globally unique. To sign assembly with strong name you need to generate the uniques key from Visual studio command prompt and assign that key to assembly.

2) Global Assembly Cache (GAC)

You can call it as centralized repository of assembly on a Machine. Each computer where a runtime is installed has a GAC. You can share assembly by installing then into GAC. Once you install assembly in GAC, it becomes global. All the application on the same machine can reference that assembly. When you add reference of any assembly from GAC into your application and when you build your application the local copy of DLL will not created in bin/debug or bin/release folder of application along with executable file. It will reference it from GAC. You can install assembly to GAC in two ways.

1) With the development tool called Gacutil.exe
2) Using installer which works with GAC.

Now we will see how to do this using a simple example. Open a visual studio and create a class library project. Add following function to the class and build it.

public string MyFunction() {
return "This is from old version";
}

Now select all files options in solution explorer and go to properties folder. Here you will find AssemblyInfo.cs file open it and see the following lines in the code.

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

This gives you assembly version by default it is 1.0.0.0 version. Now let's assign strong name to assembly. Right click on project select project properties. Go to last tab Signing. Here you will find one checkbox sign the assembly. check it and it will enable choose a key drop down. You can generate new key or you can browse to existing key if you have already generated it using command line. Using command line you can generate it using the command sn -k.

After signing build the project again. Now you can put this DLL into global assembly cache. Open a visual studio command prompt and type the following command

gacutil.exe -i /path of dll file

This will intall the assembly in GAC. Now copy paste the project folder in windows explorer and it will generate the duplicate project for it. Open it in visual studio as we are going to make few changes in it.

Add following function to the class.

public string MyFunction() {
return "This is from new version";
}

Open the AssemblyInfo.cs file and change the following two lines and build the project.

[assembly: AssemblyVersion("1.0.0.1")]
[assembly: AssemblyFileVersion("1.0.0.1")]

So this is the new version of the same DLL. Add this also to glaobal assembly cache using command mention above in the post. Now you have two versions of the same DLL in global assembly cache and you can specify version information in line of code in application. Create new windows application project in Visual studio and add reference of first DLL to it. We have to generate config file for this windows application. By default it is not available in the application. You can use Assembly Linker tool or you can add it manually through add new item. Once you generate it through Assembly Linker tool you will see following section in the file.


<dependentAssembly>
<assemblyIdentity name="classLibrary1" publicKeyToken="c0302c47590ba429" /> 
                 <bindingRedirect oldVersion="1.0.0.0" newVersion="1.0.0.1"/>  
</dependentAssembly>
Public key token value can be different in your case. Now call the function of the class in DLL.

Class1 obj = new Class1();
MessageBox.Show(obj.MyFunction());
This should show you message box with text "This is from old version". Now make following changes to config file.


<dependentAssembly>
<assemblyIdentity name="classLibrary1" publicKeyToken="c0302c47590ba429" /> 
                 <bindingRedirect oldVersion="1.0.0.0" newVersion="1.0.0.1"/>  
</dependentAssembly>

This should show you message box with text "This is from new version". 
So this was the example of Side by side execution.

1 comment:

  1. Hi the first assemblyIdentity and the second one are exactly the same ?!!

    ReplyDelete