It is an assembly container provided by the .NET Framework especially to store assemblies shared and used by different .Net applications installed on a given computer or on a given network. It is located in a subdirectory of the root system but it can not be directly accessed except that administrator permission is given before. The GAC is accessed via the .NET configuration management console witch is represented bellow:

Figure 1

Assume that an assembly is simultaneously used and/or reused by more than one developer or shared by more than one application. So, it should be shared by them all. Therefore using GAC is a good alternative. It is possible to install the assembly in the GAC and give developers privileges access to the system root rather than to copy it into each development station. It is possible that the assembly cache contains more than one version of the same assembly but it is important to mention in this context that assemblies' names don't have an extension like *.dll or *.exe when are displayed within the GAC list. Only the assembly name appears without any extensions, because when the common language runtime CLR looks for a targeted assembly, it can precise the corresponding assembly according to the application needs.

When an application is developed against an assembly, it depends on it, therefore it needs to locate it and exploit its services when running. First of all the common language runtime CLR, witch is responsible for executing the application program, verifies if the assembly is already referenced and used, if this is not the case, it search it, at second plan, in the application bin directory, after that, it is checked in the global assembly cache GAC, in a final step, the CLR checks information about the targeted assembly in the configuration file, in this step the code base located in the configuration file gives information about the targeted assembly. When the assembly is not or can not be found, an error occurs. And a message like this can be found in the error list:

The .NET Framework 2.0 Configuration management console is opened as bellow:

Figure 3

Expand the My Computer node within the tree just at right and then select the assembly cache node then click view List of assemblies in the assembly cache hyper link.

Figure 4

The assembly list is displayed as bellow:

Figure 5

How can one install an assembly in the GAC:

First of all, let us develop a simple assembly. To do that, follow those steps

Create a new class library project and name it myAssembly for example.

Figure 6

Add this code to the editor.

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

namespace myAssembly

{

publicclassClass1

{

public Class1()

{

MessageBox.Show("You are using myAssembly");

}

}

}

Save the project.

The GAC accepts only assemblies with strong names ; there fore it is imperatively recommended to sign the assembly before adding it into GAC, otherwise when adding a none strongly assembly, a message indicates that the new assembly can not be added to the GAC appears and finally the action is failed. So, to sign myAssembly, go to myAssembly properties as shown bellow.

Figure 7

Select the signing tab and then check Sign the assembly checkbox.

Figure 8

Select New in the combo box as shown above. This dialog box appears

Figure 9

Enter the key file name with twelve characters, then enter a password with more than six characters and confirm it, then click Ok.

A file looks like this is added to the application directory with *.snk as an extension witch is a Strong Name Key abbreviation. This file contains a random pair keys and it is provided to sign the assembly. This file can be generated also using the Strong name tool sn.exe provided by .NET framework.

Build the solution, save the project and close it.

Switch to the .Net Framework management console and right click on the Assembly cache node, a context menu appears.

Click add menu item, the open dialog box appears.

Figure 10

Browse to the assembly location and then click open. The assembly will be added to the global assembly cache without any problems.

The author holds a master degree in NTIC specialized in software developement delivered by the high school of communication SUPCOM, he also holds a bachelor degree in finance delivered by the economic s... Read more