Versioning

I l @ ve RuBoard

There are a few steps that need to be followed to create versioned components. At the time of this writing, VS.NET does not support the ability to automatically create signed components . The first step is the generation of a signing key using the SN.exe utility shipped with the .NET SDK. Generally, a company will create such a key and all .NET components built will use this key to sign the component. To generate a public / private key pair, issue the following command:

 sn.exe k sams.snk 

This will create a key file that can be used to sign the assembly. With the key file created, create a new VS.NET project for a C# class library and name the project SharedComponent . Next , add a new class, SharedServer , to the project and enter the code in Listing 5.2.1.

Listing 5.2.1 SharedServer Source
 1: using System;  2:  3: namespace SharedComponent  4: {  5:     /// <summary>  6:     /// Summary description for Class1.  7:     /// </summary>  8:     public class SharedServer  9:     { 10:         //Single method to return version information 11:         public string WhoAmI() { 12:             string me = "Hello from version 1.0"; 13:             return me; 14:         } 15: 16:     } 17: } 

This small class then will be used to test the versioning support provided by the CLR loader. To build the project with signing support, modify the AssemblyInfo.cs file and add the key file information, as show in Listing 5.2.2.

Listing 5.2.2 AssemblyInfo.cs
 1: [assembly: AssemblyDelaySign(false)] 2: [assembly: AssemblyKeyFile("D:\ \ SAMS\ \ SAMS.SNK")] 3: [assembly: AssemblyKeyName("")] 

Notice that the [assembly: AssemblyKeyFile(...)] attribute is used to specify the key file used to sign the assembly. This is all there is to it. Now, build the assembly and drag the compiled DLL into the GAC directory located in C:\WINNT\assembly using the Windows Explorer. Figure 5.2.2 shows the Windows Explorer of the GAC directory and the newly installed SharedComponent.dll .

Figure 5.2.2. SharedComponent.dll in the GAC directory.

graphics/0502fig02.gif

With the shared assembly in place, it's time to create a client to use the newly created SharedComponent.dll . Create a new Windows Forms project and add a reference to the SharedComponent assembly using the Add Reference explorer. (Again, at the time of this writing, VS.NET does not support adding a reference to a component installed within the GAC. Therefore, you can add a reference to the compiled DLL by browsing for it in the bin\Release directory of the shared component. Be sure to set the properties of the shared component CopyLocal to false so that the CLR will search the GAC for the component during execution of the client.)

To test the SharedComponent , merely drag a label onto the client form and add the code shown in Listing 5.2.3 to the constructor of the form.

Listing 5.2.3 Test Harness for SharedComponent
 1: public Form1()  2: {  3:     //  4:     // Required for Windows Form Designer support  5:     //  6:     InitializeComponent();  7:  8:     //  9:     // TODO: Add any constructor code after InitializeComponent call 10:     // 11:     SharedComponent.SharedServer ss = new SharedComponent.SharedServer(); 12:     this.label1.Text = ss.WhoAmI(); 13: } 

Lines 11 and 12 create a new SharedServer object and assign the Text property of the label to the returned string of the WhoAmI method. Now, compile and run the client application to test the assembly binding. The client should look similar to the window in Figure 5.2.3.

Figure 5.2.3. Shared client window.

graphics/0502fig03.gif

The client application is bound to the current version of the shared assembly based on the version information available at the time the client is compiled. In the next segment, a newer version of the component will be created and installed into the GAC. After doing so, the client application will still use the first version of the DLL until it is configured to use a newer version.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net