Appendix B. Shared Assemblies

Assemblies can be private or shared. Private assemblies can be used by only one application, and the assemblies that the application comprises are stored together. Shared assemblies are available for use by many applications and can be stored in a shared directory, a network server, or a managed assembly store known as the global assembly cache (GAC). This appendix explains how to create a shared assembly and illustrates the benefits of providing common functionality in this manner. Private assemblies are discussed in Chapter 3.

Creating a Shared Assembly

Assembly names affect how an assembly can be used. Private assemblies have a name that is unique among the assemblies that make up the application. Shared assemblies have a strong name, which consists of the assembly name, the version, and culture information and relies on public key cryptography to ensure that the strong name is globally unique.

Strong names incorporate a digital signature to ensure that the contents of an assembly cannot be modified after creation. When an application loads an assembly, the common language runtime (CLR) checks the contents of the file to ensure that no changes have occurred.

Creating a strong name is a simple process; the first step is to create a file containing a key pair that will be used to sign the assembly. (Existing key files can be used if available.) The following command uses the Strong Name Tool (sn.exe) to generate a key file named myKey.snk in the local directory:

sn –k myKey.snk

In the following example, we’ll use the C# compiler to build a single-file assembly, so we’ll create a file named AssemblyInfo.cs that contains the global attributes required to generate the strong name. The contents of the file are listed here:

using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyKeyFile("myKey.snk")]

Our metadata specifies that the assembly can be used by any culture (the assembly is said to be culture-neutral), that this is version 1.0.0.0 of the assembly, and that the key pair can be found in the myKey.snk file. Our shared assembly will contain the StringPrinter class, stored in a file named StringPrinter.cs, the contents of which are listed here:

public class StringPrinter {
    public void printString(string messageString) {
        System.Console.WriteLine("Message: " + messageString);
    }
}

We compile the classes into an assembly, using the following command:

csc /out:StringPrinter.dll /target:library
    AssemblyInfo.cs StringPrinter.cs

When a key file is specified in the global attributes, the compiler automatically generates a strong name and includes it in the assembly metadata. We can inspect the signature of the assembly by using the Strong Name Tool (sn.exe), as follows:

sn -T StringPrinter.dll

The Strong Name Tool prints the following. (Note that your public key token will be different.)

Microsoft (R) .NET Framework Strong Name Utility  Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Public key token is 539a6301c167d487
..................Content has been hidden....................

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