Shared Assemblies

Chapter 4, "Components and .NET Assemblies," covered the creation of assemblies. A shared assembly is stored in the Global Assembly Cache (GAC) so that it may be used by several applications. Because the GAC allows multiple versions of the same assembly to be present at once, the assembly must be designated using a strong name. A strong name includes the assembly's text name (the name of the file without its extension), full version number, culture information (either neutral or localized), and a unique digital signature. The following subsections discuss how to digitally sign an assembly and how to add one to the GAC.

Signing the Assembly

A digital signature is created using a public key encryption process. First you must create a public/private key pair using the Strong Name utility ( sn.exe ). When you sign an assembly, the Common Language Runtime (CLR) calculates a hash of the assembly, and then encrypts the hash using the private key. The encrypted hash value and public key are accessed within the assembly's manifest by the CLR and used to verify that the assembly remains uncorrupted and unique.

graphics/tip_icon.gif

When signing multifile assemblies, you only need to sign the assembly manifest because it contains hash values for the included files. When the signed manifest is verified as unchanged, it is easy to then evaluate the included files to ensure they are also correct.


Immediate Signing

You can create a public/private key pair using the Strong Name command-line utility provided within the .NET Framework Software Developer Kit (SDK) using this format:

 sn k MyKeys.snk 

You should then add this file to your component project and use the AssemblyVersion and AssemblyKeyFile settings to specify the version number and keyfile ( .snk ) to be used when signing this assembly. The settings will look something like this:

 <Assembly: AssemblyVersion("1.0.*.*")> <Assembly: AssemblyKeyFile("<Path to Keyfile>\MyKeys.snk")> 

When you rebuild the component, its digital signature will be created to provide a unique strong name. The use of asterisks ( * ) for the build and revision numbers allows the compiler to update those values automatically each time you rebuild the component.

Delay Signing

It is also possible to sign an assembly using only its public key so that it can later be uniquely signed using a more secured private key. This eliminates the need to provide both public and private keys to all developers.

You can extract just the public key from your keyfile using the following:

 sn.exe p MyKeys.snk MyPubKey.snk 

When using this file to sign your assembly, you must specify the use of a delay sign, as shown here:

 <Assembly: AssemblyVersion("1.0.*.*")> <Assembly: AssemblyDelaySign(True)> <Assembly: AssemblyKeyFile("<Path to Keyfile>\MyPubKey.snk")> 

After development has been completed, you may then re-sign the final component using the following private key:

 sn.exe R MyComponent.dll MyKeys.snk 
Authenticode Signing

After signing your assembly to create its strong name, you may also use a third-party digital Authenticode certificate to sign the component using the signcode.exe utility. Authenticode certificates are purchased from vendors such as VeriSign and Thawte. An Authenticode signature is used to verify your identity, in contrast to a strong name, which is used to verify the integrity of the assembly.

Adding an Assembly to the GAC

Once you have associated a strong name with your new assembly, you may add it to the GAC in several ways:

  • Windows Installer You can use the File System Editor within Visual Studio .NET to specify that a particular assembly should be installed to the GAC rather than to the application's target folder. The Windows Installer will then add the shared assembly to the installation host's GAC during installation.

  • Windows Explorer During the installation of the .NET Framework, the Assembly Cache Viewer Shell Extension ( shfusion.dll ) is installed. This component allows you to add new assemblies into the GAC by simply navigating to the assembly cache ( %systemroot%\WINNT\assembly or %systemroot%\Windows\assembly ) and then dragging and dropping the component ( .dll ) file into this folder.

  • Microsoft .NET Framework Configuration tool You may use the Add an Assembly option within the .NET Framework Configuration Tool to add your component to the GAC. This utility can be found under Start, Control Panel, Administrative Tools, .NET Framework Configuration Tool.

  • Global Assembly Cache Tool ( GacUtil.exe ) This command-line utility may be used to add or remove an assembly to or from the GAC. The utility accepts switches to install ( /i ), list ( /l ), or uninstall ( /u ) assemblies within the GAC.

graphics/note_icon.gif

After you add an assembly to the GAC, it will not appear in the Add References dialog box of Visual Studio unless you first add the assembly's folder as the default key of the assembly beneath HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders in the Registry.




Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

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