This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.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.

Global Assembly Cache Folder

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.

30 November 2008