Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

What is GAC. How and when to install an assembly into GAC - Part 4

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

4 comments:

  1. Very Nice Information.. Thanks a lot, you are doing such a wonderful job..

    ReplyDelete
  2. but sir i found many assemblies with the same name and same token number how can it possible
    Please Reply

    ReplyDelete
  3. Even they have same name and version but they will different there culture like some in English some in Russian etc.Thanks to Venkat sir who give this valuable videos.

    ReplyDelete
  4. if we are having assembly name as prj1 in 3.5 net and prj1 in 4.0.net ..
    So how to uninstall assembly from gac ?
    Do they (prj1) get removed from both

    C:\Windows\Assembly,
    and
    C:\WINDOWS\Microsoft.NET\assembly

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.