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.
Solution files for the versioning project.
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”.
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 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”.
Rename to Assembly Version 1.1
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).
Two version of “ClassLibraryVersion” dll.
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.
Client code calling the GAC class.
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. So 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 what ever 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. So the client will display “This is old Version”.
App.config file using the BindingRedirect
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”.
Different Display depending on version numbers
Note: - Source code is provided in “versioning” folder. But as you compile the DLL’s different publicToken numbers are created so you need to run the sn.exe in your machine and change the token number accordingly in the “App.config” file.
No comments:
Post a Comment