23.4. Versioning

In Section 23.3 you learned how to create an application that references a GAC assembly.

Sharing libraries among applications brings home the age-old problem regarding which version of the common library is being used by which application. Can applications that use two different versions of the same DLL coexist? How do you upgrade an application to use the new version of the DLL? Fortunately, .NET answers all these questions.

To illustrate versioning, we will create a new Hello.dll consisting of an AssemblyInfo.cs (Listing 23.7) with a different strong name and a different HelloWorld.cs (Listing 23.8).

Listing 23.7. Modified AssemblyInfo with Strong Name Components (C#)
using System.Reflection;
using System.Runtime.CompilerServices;

[assembly: AssemblyTitle("MyApp")]
[assembly: AssemblyDescription("A simple app")]
[assembly: AssemblyCompany("MyCompany.com")]
[assembly: AssemblyProduct("MyApp")]
[assembly: AssemblyCopyright("This is the property of
MyCompany.com")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
						[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyKeyFile("Hello.snk")]

Note that in Listing 23.7 we need only change the version number in order to change the strong name of an assembly. Next, we create a slightly different HelloWorld class.

Listing 23.8. A Slightly Different HelloWorld Class (C#)
public class HelloWorld {
  public override string ToString() {
  return "A slightly different Hello World";
  }
}

Next, we quickly create Hello.dll, register it in the GAC, and compile the HelloWorldDriver class using this new DLL.

csc /target:library /out:Hello.dll HelloWorld.cs AssemblyInfo.cs
gacutil /i Hello.dll
csc /target:exe /out:MyApp.exe HelloWorldDriver.cs
/reference:Hello.dll

Now type MyApp, and you should see the following output:

A slightly different Hello World

To list the contents of the GAC, you type gacutil /l. This will output all the shared assemblies in your environment. You will see the two versions of Hello.dll in the output:

Hello, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc6fce1c7117f564om=null

Hello, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=cc6fce1c7117f564om=null

Note that the version information is different for the two assemblies.

Now that we have two assemblies in the GAC, how do we force an application to use the desired version of the assembly? The .NET Framework supports assembly policy files, which are XML files that indicate how the assembly is to be upgraded. A sample policy file is shown in Listing 23.9.

Listing 23.9. A Sample .NET Policy File (C#)
<configuration>
  <runtime>
    <assemblyBinding>
      <dependentAssembly>
        <assemblyIdentity name="Hello"
          publicKeyToken="cc6fce1c7117f564"
                              culture=""/>
          <bindingRedirect oldVersion="1.0.0.0"
                        newVersion="2.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

The new policy-based Hello.dll is then created using the following command:

al /link:policy.xml /keyfile:Hello.snk /out:policy.1.0.Hello.dll

Note that the name format of the new Hello.dll includes the word policy with a number indicating clearly that the assembly enforces a policy. The next step is to register this policy-based DLL in the GAC:

gacutil /i policy.1.0.Hello.dll

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.46.229