 .NET 1.1+Adding Assemblies to the Global Assembly Cache
The global assembly cache (GAC) provides a centralised, machine-wide storage location for .NET assemblies. When you add an assembly to the GAC, you allow it to be shared by many programs, rather than requiring a copy to be installed for each application.
Shared Assemblies
The principle of code reuse, to minimise the processes of duplicating code between software applications, often leads to the creation of shared class libraries. To maximise the potential for reuse, you will generally isolate a class library within an assembly, or a group of related assemblies, and package it into a dynamic linked library, or DLL. This DLL can then be referenced from within many other class libraries or software applications.
By default, when you add a reference to a DLL to your project, the reference's "Copy Local" property is set to true. On building the solution, the DLL will therefore be saved into the folder containing the compiled program. The entire output folder can then be simply copied to the end-user's computer and executed, as all of the required assemblies are available. The downside to this approach is that each program deployed includes its own copy of the DLL. If the shared classes are updated to fix bugs or to implement new features, the DLL may need to be deployed to multiple locations to ensure that every program is using the latest revision.
The Global Assembly Cache
The Global Assembly Cache, or GAC, provides a solution to the problem of duplicated DLLs. The GAC is a machine-wide repository for shared assemblies. Once an assembly is added to the GAC, it is available to all applications that wish to use it. To avoid the historical problems of DLL Hell, the GAC can hold multiple versions of the same assembly. When an assembly is requested, the common language runtime (CLR) will ensure that the correct version is loaded.
The use of the global assembly cache for storing your shared assemblies has some drawbacks. One of the more important problems is that when your programs rely on assemblies being registered in the GAC, they become more difficult to deploy. Specifically, it is no longer possible to install your application simply by copying the output folder to the target machine. Another limitation is that assemblies may only be added to the cache if they are signed with a strong name key.
It is recommended that you only use the GAC for your own assemblies when it is absolutely necessary. Microsoft suggests that you should generally keep your assemblies within the application folder. However, there are several good reasons to use the GAC, including:
- storing assemblies that are shared by multiple applications.
- enforcing the security settings that are applied to shared assemblies. The GAC is provided as a folder within the file system and, as such, can be controlled using standard NT file permissions.
- maintaining multiple versions of the same assembly in a single location.
- providing an additional search location for assemblies. When a program loads an assembly, it looks in the GAC first to find the correct version, before probing the file system if necessary.
Viewing the GAC's Contents
The Global Assembly Cache appears as a folder named "assembly" within the file system. The folder is found in the Windows folder (or WINNT folder depending upon the version of Windows in use). When browsed using Windows Explorer, the names, versions, public key tokens and other details of all of the installed assemblies are displayed.

Installing an Assembly into the GAC
There are many methods for installing an assembly into the global assembly cache. The preferred method is to create an installation package that utilises the Windows Installer software to register the assembly. Windows Installer provides many benefits including performing instance counting so that when all applications that use the DLL are removed, the DLL is also deleted. It also provides facilities for rolling back partial installations correctly and installing the assembly only if it does not already exist in the GAC. However, using an installation package is beyond the scope of this article.
If you are not using an installer, there are two easy ways to install an assembly to the GAC. These are using the .NET Global Assembly Cache Utility (gacutil.exe) or by copying the DLL using Windows Explorer. Although there are other ways to achieve the same results, I will describe these two variations in this article.
NB: You must have the relevant permissions to install and uninstall assemblies in the GAC. If you are using Vista, run the gacutil.exe tool using the Run as Administrator option. For copying using Windows Explorer, you must have write permissions on the GAC folder.
Installing Using the GAC Utility
The Global Assembly Cache Utility is a command line tool that should be executed from the Visual Studio command prompt. The utility provides various functions, including allowing an assembly to be installed into the GAC and uninstalled at a later time. To install an assembly to the GAC, use the -i switch and provide the name of the DLL file, including the path if the file is not in the current folder. For example, to install an assembly named "MyAssembly" held in a file named "MyAssembly.dll", you would execute the following command:
gacutil -i MyAssembly.dll
After you install your assembly, you can view it by browsing to the assembly folder. To remove an assembly that is no longer required, use the -u switch. When uninstalling, the name of the assembly, not the DLL, must be provided. For example:
gacutil -u MyAssembly
Copying Assemblies into the GAC
A second manner in which to install an assembly into the GAC is using Windows Explorer. In this case, the DLL file can simply be copied into the assembly folder. To uninstall the assembly, select it and delete it from the folder.
Referencing a GAC Assembly in Visual Studio
Even when your assembly will be held in the GAC, you must still provide a reference to it during development. You can add the reference in the same way that you would any other. However, once included in your project, you should select the reference in the Solution Explorer and set its "Copy Local" property to false. This will prevent the DLL file from being copied to the output folder when you compile your software.
When you copy your compiled software folder to the target machine, the version of the shared assembly in the GAC will be used automatically. If the required assembly is not correctly registered in the GAC, you will receive a run-time error when it is accessed. This error indicates that the CLR, "Could not load file or assembly". If you see this error, ensure that your assembly is correctly installed.
|