In the AssemblyInfo.cs file, locate the following lines:

...

// You can specify all the values or you can default the Build and Revision Numbers

// by using the '*' as shown below:

// [assembly: AssemblyVersion('1.0.*')]

[assembly: AssemblyVersion('1.0.0.0')]

[assembly: AssemblyFileVersion('1.0.0.0')]

The version number of an assembly is specified using the following format:

[Major Version, Minor Version, Build Number, Revision]

The AssemblyVersion attribute is used to identify the version number of an assembly. Applications that use this particular assembly reference this version number. If this version number is changed, applications using this assembly will not be able to find it and will break.

The AssemblyFileVersion attribute is used to specify the version number of the assembly, and it shows up in the properties page of the assembly (more on this in a later section).

Building the Assembly

Build the Class Library project so that Visual Studio 2008 will now generate the shared assembly and sign it with the strong name. To examine the shared assembly created, navigate to the binDebug folder of the project and type in the following command:

ildasm Base64Codec.dll

Figure 15-32 shows the public key stored in the manifest of the shared assembly.

Figure 15-32 

You can obtain the public key token of the shared assembly by using the following command:

sn -T Base64Codec.dll

Figure 15-33 shows the public key token displayed in the console window. Note this number because you will use it for comparison later.

Figure 15-33

The Global Assembly Cache

Now that you have created a shared assembly, the next task is to put it into the GAC. The GAC is a central repository of .NET assemblies that can be shared by all applications. There are several reasons why you should put your shared assembly into the GAC, some of which are:

□ Security — Assemblies stored in the GAC are required to be signed with a cryptographic key. This makes it difficult for others to tamper with your assembly, such as replacing or injecting your shared assembly with malicious code.

□ Version management — Multiple versions of the same assembly can reside in the GAC so that each application can find and use the version of your assembly to which it was compiled. This helps to avoid DLL Hell, where applications compiled to different versions of your assembly can potentially break because they are all forced to use a single version of your assembly.

□ Faster loading — Assemblies are verified when they are first installed in the GAC, eliminating the need to verify an assembly each time it is loaded from the GAC. This improves the startup speed of your application if you load many shared assemblies.

The GAC is located in <windows_directory>Assembly. In most cases, it is C:WindowsAssembly. When you navigate to this folder by using Windows Explorer, the Assembly Cache Viewer launches to display the list of assemblies stored in it (see Figure 15-34).

Figure 15-34

Putting the Shared Assembly into GAC

To put the shared assembly that you have just built into the GAC, drag and drop it onto the Assembly Cache Viewer. Alternatively, you can also use the gacutil.exe utility to install the shared assembly into the GAC (see Figure 15-35):

gacutil /i Base64Codec.dll

Figure 15-35 

If you are using Windows Vista, make sure to run the command prompt as Administrator.

If the installation is successful, you will see the shared assembly in the Assembly Cache Viewer (see Figure 15-36).

Figure 15-36

The version number displayed next to the DLL is specified by using the AssemblyVersion attribute in the AssemblyInfo.cs file (as discussed earlier). Select the Base64Codec DLL, and click the Properties button (the button with the tick icon) to see the Properties page as shown in Figure 15-37.

Figure 15-37

The version number displayed in this page is specified using the AssemblyFileVersion attribute.

To install different versions of the same assembly to the GAC, simply modify the version number in AssemblyInfo.cs (via the AssemblyVersion attribute), recompile the assembly, and install it into the GAC.

Physically, the shared assembly is copied to a folder located under the GAC_MSIL subfolder of the GAC, in the following format:

<Windows_Directory>assemblyGAC_MSIL<Assembly_Name> <Version>_<Public_Key_Token>

In this example, it is located in:

C:WindowsassemblyGAC_MSILBase64Codec1.0.0.0_2a7dec4fb0bb6

Figure 15-38 shows the physical location of the Base64Codec.dll assembly.

Figure 15-38

Making the Shared Assembly Visible in Visual Studio

By default, adding a shared assembly into the GAC does not make it appear automatically in Visual Studio's Add Reference dialog. You need to add a registry key for that to happen. Here's how to handle that.

First, launch the registry editor by typing regedit in the Run command box.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату