This multi-part article answers common questions about assemblies, the basic building blocks of .NET applications. This Part 4 covers shared assemblies and the Global Assembly Cache.

Where are shared assemblies stored?

A shared assembly is used by multiple applications. You can store shared assemblies pretty much anywhere. However, the challenge is to ensure that all dependent applications can find the shared assembly. The recommended way to ensure this is to store shared assemblies in the Global Assembly Cache.

What is the Global Assembly Cache (GAC)?

The Global Assembly Cache is a system folder (typically C:Windowsassembly) that contains .NET shared assemblies. Companies that wish to share assemblies with others or even just among their own applications typically store these shared assemblies in the GAC. All of the .NET framework libraries are stored in the GAC.

Why should I store my shared assemblies in the GAC?

You should install assemblies in the GAC only when necessary. As a general guideline, assemblies should be kept private and stored in the application’s folder unless you explicitly need to share them. There are some benefits to storing shared assemblies in the GAC:

Global Location

The GAC is the known standard location for .NET shared assemblies. When an application attempts to load an assembly, the GAC is one of the first places it looks. If there’s any chance that an application outside your control may someday require access to your shared assembly, you should install your assembly in the GAC so the application is sure to find it.

Security

The GAC is a system folder typically protected by administrator rights. Once an assembly is installed in the GAC, it cannot be easily modified. Also, assemblies stored in the GAC must be signed with a cryptographic key. These protections make it difficult to spoof your assembly, in other words, replace or inject your assembly with a virus or malicious code.

Version Management

.NET allows multiple versions of the same assembly to reside in the GAC so that each application can find and use the version of your assembly to which it was compiled. This helps avoid DLL Hell, where applications that may be compiled to different versions of your assembly could potentially break because they are all forced to use a single version of your assembly.

Faster Loading

The system verifies assemblies when they are first installed in the GAC, eliminating the need to verify an assembly each time it is loaded from the GAC. This can improve the startup speed of your application if you load many shared assemblies.

Why would I avoid the GAC?

The GAC should contain “global” shared assemblies only, so there are many instances when you would NOT install an assembly in the GAC:

  • The assembly is private to your application and not to be shared with other applications.
  • You want to use XCOPY or FTP copy to install a .NET application to a single folder. This eliminates the need to access the Registry and GAC and does not require administrator rights.
  • The assembly is not strong-named or you do not want tight version control.
  • COM interop and unmanaged code do not require the GAC.

How do I see assemblies installed in the GAC?

The .NET Framework includes an Assembly Cache Viewer. Open Windows Explorer, enter %windir%assembly in the address bar, and all global assemblies will appear in a special view that shows the assembly name, version, culture, public key token, and processor architecture.

Global Assembly Cache

Can I install multiple versions of the same assembly in the GAC?

Yes. Normally you would not be able to have two files with the same name in a Windows folder, but the GAC is a special folder that stores its contents by strong name. Hence, two assemblies with the same name but different versions or cultures may coexist in the GAC.

How do I add/remove assemblies from the GAC?

Assemblies added to the GAC must be signed with a strong name. There are multiple ways to add/remove assemblies from the GAC:

Windows Installer

The preferred way to add/remove assemblies from the GAC is with Microsoft Windows Installer 2.0. Visual Studio includes a limited version of Windows Installer, and most major setup programs such as InstallShield also use Windows Installer. There are benefits to using Windows Installer:

  • Windows Installer provides a simple interface for developers to add/remove shared assemblies in the GAC and can handle private assemblies as well.
  • Installer provides a familiar interface and setup experience for the user.
  • Installer can also install application shortcuts and supporting files such as ReadMe and license agreements and can run other installation programs and scripts.
  • Installer registers and tracks references to assemblies installed in the GAC to determine which assemblies are still required.
  • Installer can repair and patch assemblies and rollback unsuccessful installations.
  • Installer can install assemblies on-demand as they are needed by applications.

GAC Utility

The .NET developer’s kit includes a command line utility GACutil.exe to interact with the GAC. This utility is intended for use in a development environment only and should not be used to install assemblies on a client PC because:

  • The GACutil license agreement states that it is not freely distributable.
  • GACutil is part of the .NET SDK, which may not be installed on many target PCs.
  • GACutil lacks many important features found in Windows Installer such as assembly repair and rollback.

Assembly Cache Viewer

Using the Assembly Cache Viewer shown above, you can drag & drop assemblies from any folder into the GAC and also delete assemblies installed in the GAC.

.NET Framework Configuration Administrative Tool

To access the .NET Framework Configuration Tool:

  1. Click Start > Control Panel.
  2. Double-click on Administrative Tools.
  3. Double-click on Microsoft .NET Framework 2.0 Configuration.
  4. Ensure My Computer is selected in the tree.
  5. In the Tasks group, click the Manage the Assembly Cache link.
  6. Two links appear, enabling you to view and add assemblies in the GAC.

How do I access the GAC programmatically?

You can access the GAC from code with the fusion.dll library. Here is an excellent C# wrapper for the GAC.

You are strongly advised NOT to access the GAC from code unless you are creating an administrative or setup tool. The Fusion APIs expose your application to the inner workings of assembly binding and may cause your application to fail on future .NET versions.

How do I add my shared assembly to the Visual Studio “Add Reference” dialog?

If you add an assembly to the GAC, it will NOT automatically appear in the Visual Studio “Add Reference” dialog; instead you must add your assembly manually.

How do I move the GAC?

When installing .NET, you cannot configure the GAC location, however you can move the GAC after it is installed. See this article and scroll down to “Relocating the GAC.”

Links

.NET Assembly FAQ – Part 1
.NET Assembly FAQ – Part 2 – Attributes
.NET Assembly FAQ – Part 3 – Strong Names and Signing
.NET Assembly FAQ – Part 5 – coming soon