GAC
GAC stands for Global Assembly Cache and contains strong named assemblies. Assemblies in the GAC can be shared by all applications running on that machine, without having to copy the assembly locally. It is recommended to install an assembly into GAC, only when required and shared by applications, otherwise they should be kept private. You shouldn’t add an assembly into the GAC, if you wish to deploy your application to another machine using XCopy deployment. This is because in XCopy deployment, we only copy the application files to the target machine and not the GAC contents. XCopy deployment is simply copying files from one location to another.
With the introduction of .NET 4.0, we have 2 GAC’s. One for DotNet 2.0 to 3.5 assemblies and the other for .NET 4.0 assemblies. The following are the paths for the 2 GAC’s
1. C:\Windows\Assembly – For .NET 2.0 – 3.5 assemblies
2. C:\WINDOWS\Microsoft.NET\assembly – For .NET 4.0 assemblies
To install an assembly into the GAC, the assembly must be strongly named, otherwise you get an error stating – Failure adding assembly to the cache: Attempt to install an assembly without a strong name. There are 2 ways to install an assembly into GAC.
1. Simply Drag and Drop
2. Use GacUtil.exe (GAC Utility Tool)
To install an assembly using gacutil, use the following command. This command installs SampleAssembly.dll into the GAC. If you have build this project using .NET framwork 4.0 then look in C:\WINDOWS\Microsoft.NET\assembly, else look in C:\Windows\Assembly.
Gacutil -i C:\SampleProject\SampleAssembly.dll
Note: If you are using Visual Studio 2010, then by default the target framework for any new project is .NET 4.0. If you want to change the target framework, right click the project and select properties. In the properties window, you can change the target framework version.
To uninstall an assembly from the GAC, using GAC utility, use the following command.
Gacutil -u MyClassLibrary
If there are multiple versions of MyClassLibrary assembly, in the GAC, then all these versions will be removed by the above command. If you want to remove only one of the assemblies then specify the full name as shown below.
gacutil -u ClassLibrary,Version=1.0.0.0,PublicKeyToken=eeaabf36d7783129
Note: Please make sure there are no spaces between Comma(,) and the words “Version” and PublicKeyToken, otherwise you get an error stating Unknown option: Version=1.0.0.0. Also, don’t specify the assembly extension (.dll or .exe) when uninstalling, otherwise the assembly will not be uninstalled. You will just get a message stating Number of assemblies uninstalled = 0