a blog for those who code

Wednesday 23 December 2015

Understanding Global Assembly Cache (GAC)

In this post we will be discussing about Global Assembly Cache (GAC). Global Assembly Cache is nothing but a shared location in your machine that keeps assemblies. GAC (Global Assembly Cache) is where all shared .Net assembly resides. Every computer which has CLR installed has a machine wide code cache called GAC.

If an assembly has to be accessed by multiple applications, the assembly must be placed into a well-known directory, and CLR must know to look in this directory automatically when a reference to the assembly is detected. This location is called Global Assembly Cache (a.k.a GAC). The exact location of GAC is subject to change based on the versions of the .Net Framework.

Previous CLR's uses %SystemRoot%\Assembly as its common locations for DLL's. These are upto .Net Framework 3.5. After .Net Framework 4.0 the directory is changed to %SystemRoot%\Microsoft.NET\Assembly.

The GAC directory is structured. It contains sub directories like GAC_32, GAX_64, GAC_MSIL. You should never manually copy the DLL's into the GAC, instead use tool to accomplish this task. Now if you want to deploy a strongly named assembly in the GAC, you can use the tool GACUtil.exe. Running GACUtil.exe without any command-line arguments yields the following usage.


As you can see, if you invoke GACUtil.exe specifying the /i switch, it will install an assembly into the GAC and you can use /u switch to uninstall an assembly from the GAC.

You can only install a Strongly Named Assembly into GAC. If you try to install any assembly other than strongly typed assembly it throws an error like "Failure adding assembly to the cache: Attempt to install an assembly without a strong name".

Importance of Global Assembly Cache (GAC)

GAC solves the problem of DLL Hell and DLL versioning. When a .Net component is installed in a system,  GAC looks at its version and creates a strong name for the component. The component then registered in the repository and indexed by its strong name rather than the version number. So there will be no confusion if more than one version of a component is present in a system.

Please Like and Share the CodingDefined.com Blog, if you find it interesting and helpful.

2 comments:

  1. Replies
    1. Chris, its not exactly true...there are some instances where you need to package a lot of assemblies together, at that time packaging through Nuget becomes cumbersome. So GAC is better option to deal with this type of scenario.

      Delete