Global Assembly Cache (GAC) in .NET

Definition GAC stands for Global assembly cache and it contains all the public assemblies.

GAC is a folder in windows, containing all the public assemblies in a machine,
so that these assemblies can be used by all the applications running in a system.

Example of an application using an assembly from GAC

Consider a console application with code as follows

Console.WriteLine("HelloWorld")
In the above code WriteLine is a method present in the Console class,
Console is a class present in the System assembly,
System Assembly is present inside the GAC

we can see that System assembly is in the references folder of the Console Application project we  have created.

Here System assembly will be automatically installed into GAC when we install the .net framework.

Why an Assembly needs to be added inside GAC?

Consider the following scenario

There are two project in one solution , Project1 and Project2
When we add the refernece of Project1 in the Project2 then what actually happens is that the Project1 assemblies will be copied to the bin folder of the Project2.

Now if the same Project1 assemblies is to be used in 10 different Project of Same/Different solutions, then the reference of the Project1 assemblies needs to be added to the new projects.
Here when we add the reference, the Project1 assemblies gets physically copied (when copy local is true ) to bin folders of the all the New Projects.

To avoid this copying of Project 1 assemblies into all the projects we put the Project1 assemblies into GAC and then we refer the Assemblies from GAC in our new projects

Advantages
The advantage of adding an assembly into GAC is that the assembly can be shared between multiple applications without having the need to copy  the assembly into each of the project where we use the assembly

When to install an assembly into GAC

1) When the Assemblies are to be used by different applications running in the same machine.

When not to install an assembly into GAC

1) If the deployment is to be done in another machine using XCopy method,
since in the Xcopy method the contents of GAC are not copied.
2) When we still need to do changes to the assembly

GAC PATH

PATH for GAC from 2.0 to 3.5 is

%windir%\assembly\
ie all the assemblies gets installed in the above path

PATH for GAC from .net 4.0 is

%windir%\Microsoft.NET\assembly\GAC_MSIL

ie all the assemblies gets installed in the above path
Here It implies that we have two GACs from .NET framework 4.0

Note - Only strong named assemblies can be installed in the GAC.
A assembly is said to be strong named if the assembly contains
1)Name
2)Version Number
3)Culture
4)Public key

Ways of installing an assembly into GAC
1)Drag and Drop
2)Using a utility called gacutil.exe

gacutil command
i) To Install an assembly into GAC (with dll)
gacutil.exe -i SampleAssembly.dll

ii) To Check Whether an assembly is present inside a GAC (with or without dll)
gacutil.exe /l SampleAssembly

iii) To Unistall an assembly from GAC (should be without the extension ie dll)
gacutil.exe -u SampleAssembly

iv) To Unistall an assembly of specific version from GAC (should be without the extension ie dll)
gacutil /u "SampleAssembly,Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789ABCDEF"

Note - To install an assembly into GAC the first we have to navigate to the path where the assembly is present.
ie if the assembly SampleAssembly.dll is present in the folder SampleApplication in C drive, then first we have to navigate to the SampleApplication folder then we need to execute the gacutil command


Also we can provide the absolute path of the dll and then install it.

2) To perform the install and uninstall operation we need to run the visual studio command prompt in Administrator mode.
(or set the path to the gacutil command in windows and use the normal command prompt)

Note - Copy Local property of the assembly from the GAC is false since
its being referenced from the GAC and hence its not copied to the bin folder
of our application.