Making choice of same assembly with different version
If we have two version of same assembly in GAC how do we make a choice?
OK first let us try to understand what the interviewer is talking about. Let us say you have made an application and its using a DLL which is present in GAC. Now for some reason you make second version of the same DLL and put it in GAC. Now which DLL does the application refer? Ok by default, it always uses the version by which you have compiled. However, you want that it should actually use the older version. So first, we answer in short. You need to specify “bindingRedirect” in your config file. For instance in the below case “ClassLibraryVersion” has two versions “1.1.1830.10493” and“1.0.1830.10461” from which “1.1.1830.10493” is the recent version. However, using the bindingRedirect we can specify saying “1.0.1830.10461” is the new version. Therefore, the client will not use “1.1.1830.10493”.
Ok now we will try to answer it in long way by doing a small sample project. Again, this project will be done using C#. Below is the solution display, it has two projects one the windows client project (“WindowsVersioningCSharp”) and second the class library project (“ClassLibraryVersion”) which will be installed in GAC with two versions.
Our first primary goal is to put two different versions of the same DLL in GAC. So let us make a walk through of “ClassLibraryVersion” project. It is a very simple class that has “Version” function, which just sends a string “This is old Version”. Second, we will also just ensure that the assembly version is “1.0” in the “AssemblyInfo.cs”.
Second, in order that we can put a DLL in GAC we need to create generate strong names and assign the same to the class. For instance, in below figure I have generated the strong name in “mykey.snk” and assigned the same to the DLL.
Finally, we need to install the same in GAC using “gacutil” tool. Below is the figure that shows the same. This installs one version of “ClassLibraryVersion.dll” in GAC.
Now it is time to create a second version of the DLL. So here is what we will do first, we will just return a different string value for this new version DLL. You can see in the below figure I have changed the string to return “This is New Version”. Secondly we also need to change the AssemblyVersion to “1.1.*” in the “AssemblyInfo.cs” file. After that again compile the DLL and run the “gacutil” to register this second version of the “ClasLibraryVersion.dll”.
Now when we view the GAC we can see two version of “ClassLibraryVersion” i.e. “1.1.1832.2619” and “1.0.1832.2172” (see figure below).
Now that we have created the environment of two version of the same DLL in GAC its time to look at how client can make a choice between those versions. We need to generate “publicKeyToken” in order to move ahead. Below is a sample print screen, which shows how we can use “sn.exe” to generated the public key token. Note the “-T” parameter.
Now let us look at the client that will consume this DLL. I have just added windows form and a button to the same. In the button click, we will try to call the version function and display the data. Therefore, below is the code in the first step we create the object of “ClassLibraryVersion.Class1” and in the second step we call the “Version” function to display the data.
Now comes the most important part of the whole thing the “app.config” file, which will decide which version, should be used. So add a new “app.config” file in the project and add the “AssemblyBinding” section as show below. Therefore, you need to specify the following things:-
- Assembly name in the “name” attribute of “assemblyIdentity” section.
- Specify the “publicKeyToken” value in the “assemblyIndentity”section which was generated using “sn.exe –T ‘dllname.dll’ “.
- Specify the “oldVersion” and “newVersion” values in the “bindingRedirect” element. So whatever version we want the client to use should be specified in the “newVersion” attribute.
You can see from the figure below I have specified that client should use “1.0.*” version. Therefore, the client will display “This is old Version”.
If you run the source code with changing version numbers you can see the below two message boxes on different version numbers.” This is old version” will be displayed when “newVersion” value is “1.0.1832.5411” and “This is new Version” will be displayed when “newVersion” value is “1.1.1832.5427”.