Global Assembly Cache Explained

The Global Assembly Cache (GAC) is a machine-wide CLI assembly cache for the Common Language Infrastructure (CLI) in Microsoft's .NET Framework. The approach of having a specially controlled central repository addresses the flaws in the shared library concept and helps to avoid pitfalls of other solutions that led to drawbacks like DLL hell.

Requirements

Assemblies residing in the GAC must adhere to a specific versioning scheme which allows for side-by-side execution of different code versions. Specifically, such assemblies must be strongly named.

Usage

There are two ways to interact with the GAC: the Global Assembly Cache Tool (gacutil.exe) and the Assembly Cache Viewer (shfusion.dll).

Global Assembly Cache Tool

gacutil.exe is an older command-line utility that shipped with .NET 1.1 and is still available with the .NET SDK.[1]

One can check the availability of a shared assembly in GAC by using the command:

gacutil.exe /l <assemblyName>

One can register a shared assembly in the GAC by using the command:

gacutil.exe /i <assemblyName>

Or by copying an assembly file into the following location:

%windir%\assembly\
Note that for .NET 4.0 the GAC location is now:
%windir%\Microsoft.NET\assembly\

Other options for this utility will be briefly described if you use the /? flag, i.e.:

gacutil.exe /?

Assembly Cache Viewer

The newer interface, the Assembly Cache Viewer, is integrated into Windows Explorer.[2] Browsing %windir%\assembly\ (for example, C:\WINDOWS\assembly) or %WINDIR%\Microsoft.NET\assembly, displays the assemblies contained in the cache along with their versions, culture, public key token, and processor architecture. Assemblies are installed by dragging and dropping and uninstalled by selecting and pressing the delete key or using the context menu.

With the launch of the .NET Framework 4, the Assembly Cache Viewer shell extension is obsolete.[3]

Example of use

A computer has two CLI assemblies both named AssemblyA, but one is version 1.0 and the other is version 2.0. Since it is required that both be compiled to a file named AssemblyA, they cannot exist in the same directory within the FAT32 file system. Instead, the virtual file system of the GAC can be used by programs that need to use each version of the assembly specifically.

Implementation

The GAC as a construct does not actually exist within the Windows OS. It is implemented and managed by the CLI. The folders within %systemroot% named assembly and Microsoft.NET\assembly (for .NET 4.0) contain all globally available assemblies with managed filenames so that the version and public key tokens can be included. Each version can therefore exist within the same location and be called without requiring subsequent versions to preserve code entry point locations as usual. Windows Explorer allows the drag-and-drop installation of assemblies into this folder only if they would otherwise be permitted to be installed from the command line.

A calling application may specify a version of an assembly when referencing it, so the run-time can simply refer to the filename to use the correct one.

Pitfalls

The Global Assembly Cache mechanism helps to avoid older DLL hell, but it still has some drawbacks, such as:[4]

See also

External links

Notes and References

  1. Web site: Global Assembly Cache Tool (Gacutil.exe) . . . 2010-05-27 . 2013-07-17 . https://web.archive.org/web/20130717153819/http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.80).aspx . live .
  2. Web site: Assembly Cache Viewer (Shfusion.dll) . . . 2010-05-27 . 2013-07-30 . https://web.archive.org/web/20130730060747/http://msdn.microsoft.com/en-US/library/34149zk3(v=vs.80).aspx . live .
  3. Web site: How to: View the Contents of the Global Assembly Cache . . . 2010-07-22 . 2013-07-19 . https://web.archive.org/web/20130719042536/http://msdn.microsoft.com/en-us/library/9x295t9k.aspx . live .
  4. Web site: Ten Managed Application Pitfalls that Kill Version Compatibility . Mueller . John . devsource.com . 2005-02-11 . 2008-01-26 . dead . https://archive.today/20130121161916/http://www.devsource.com/c/a/Techniques/Ten-Managed-Application-Pitfalls-that-Kill-Version-Compatibility/ . 2013-01-21.