BlackWaspTM
.NET Framework
.NET 1.1+

Adding Assemblies to the Global Assembly Cache (2)

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.

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.

30 November 2008