Global Assembly Cache
Encyclopedia
The Global Assembly Cache or GAC is a machine-wide .NET assemblies cache
Cache
In computer engineering, a cache is a component that transparently stores data so that future requests for that data can be served faster. The data that is stored within a cache might be values that have been computed earlier or duplicates of original values that are stored elsewhere...

 for Microsoft
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...

's CLR
Common Language Runtime
The Common Language Runtime is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time compilation, the CLR compiles the intermediate language code known as CIL into the machine instructions...

 platform
Platform (computing)
A computing platform includes some sort of hardware architecture and a software framework , where the combination allows software, particularly application software, to run...

. The approach of having a specially controlled central repository addresses the shared library concept and helps to avoid pitfalls of other solutions that led to drawbacks like DLL hell
DLL hell
In computing, DLL Hell is a term for the complications that arise when working with dynamic link libraries used with Microsoft Windows operating systems, particularly legacy 16-bit editions which all run in a single memory space....

.

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

Microsoft provides 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.

One can check the availability of a shared assembly in GAC by using the command:
gacutil.exe /l 


One can register a shared assembly in the GAC by using the command:
gacutil.exe /i 


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
Windows Explorer
This article is about the Windows file system browser. For the similarly named web browser, see Internet ExplorerWindows Explorer is a file manager application that is included with releases of the Microsoft Windows operating system from Windows 95 onwards. It provides a graphical user interface...

. 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
Delete key
The delete key , known less ambiguously as forward delete, performs a function when struck on a computer keyboard during text or command editing, which is to discard the character ahead of the cursor's position, moving all following characters one position "back" towards the freed letterspace. The...

 or using the context menu
Context menu
A context menu is a menu in a graphical user interface that appears upon user interaction, such as a right mouse click or middle click mouse operation...

.

With the launch of the .Net Framework 4, the Assembly Cache Viewer shell extension is obsolete.

Example of use

A computer has two .NET 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 Allocation Table
File Allocation Table is a computer file system architecture now widely used on many computer systems and most memory cards, such as those used with digital cameras. FAT file systems are commonly found on floppy disks, flash memory cards, digital cameras, and many other portable devices because of...

 file system
File system
A file system is a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve and update data, as well as manage the available space on the device which contain it. A file system organizes data in an efficient manner and is tuned to the...

. 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 .NET Framework. 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. 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 Common Language Runtime
Common Language Runtime
The Common Language Runtime is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time compilation, the CLR compiles the intermediate language code known as CIL into the machine instructions...

 can simply refer to the filename to use the correct one.

Pitfalls

The Global Assembly Cache mechanism helps to avoid older DLL hell
DLL hell
In computing, DLL Hell is a term for the complications that arise when working with dynamic link libraries used with Microsoft Windows operating systems, particularly legacy 16-bit editions which all run in a single memory space....

, but it still has some drawbacks, such as:
  • By default, applications will only run with the version of the .NET Framework
    .NET Framework
    The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...

     used to compile it, which can cause the application to fail on machines with newer versions of the .NET Framework installed — even when the application would normally run properly with the newer version.
  • It is sometimes necessary to use Conditional compilation if some of the core .NET calls (used in the application) are only supported for some versions of the framework.
  • .NET applications that rely on native code risk incompatibilities, even with the GAC mechanism.
  • Every assembly that is added to the GAC must be strongly named
    Strong key
    Strong Key is a naming convention used in computer programming. There can be more than one component with the same naming, but with different versions. This can lead to many conflicts....

    . The process of making an assembly "strongly named" can be quite painful in some situations. For example, if an assembly depends on another assembly that is not strongly named, it cannot be registered in the GAC. In cases where the code of the 3rd party assembly is not in the programmer's propriety, transforming the assembly to be strongly named can in fact be impossible.
  • Browsing for files using standard Windows APIs doesn't allow selection of DLLs located under the "assembly" folder when Explorer is showing the user-friendly view of the GAC.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK