If we have two version of same assembly in GAC how do we make a

Q

If we have two version of same assembly in GAC how do we make a choice ?

✍: Guest

A

OK first let’s try to understand what the interviewer is talking about. Let’s 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 you application in IDE. But 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. But using the bindingRedirect we can specify saying “1.0.1830.10461” is the new version. So the client will not use “1.1.1830.10493”.

<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ClassLibraryVersion"
publicKeyToken="b035c4774706cc72"
culture="neutral"/>
<bindingRedirect oldVersion= "1.1.1830.10493"
newVersion= "1.0.1830.10461"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Ok now I will try to answer it in long way by doing a small sample project. Again this project will be done using C#. So in CD you can find the “Versioning” project. 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’s make a walk through of “ClassLibraryVersion” project. It’s a very simple class which 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”



Finally we need to install the same in GAC using “gacutil” tool. Below is the figure which 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’s look at the client which 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. So 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.

2007-10-22, 6602👍, 0💬