What is the GAC What problem does it solve?

GAC - Global Assembly Cache:

GAC has strong named assemblies. Assemblies in the GAC can be shared by all applications running on that particular machine. If any assembly used in multiple applications than its recommended to install into GAC, otherwise they should be kept private.

GAC - Global Assembly Cache location is as below

  • C:\Windows\Microsoft.NET\assembly\GAC_32
  • C:\Windows\Microsoft.NET\assembly\GAC_64
  • C:\Windows\Microsoft.NET\assembly\GAC_MSIL

There are 2 ways to install an assembly into GAC

  • Simply Drag and Drop
  • Use GacUtil.exe (GAC Utility Tool)

Note: 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

To install an assembly using GAC utility

  • gacutil-i C:\Code Sanitize\SampleDll.dll

To uninstall an assembly using GAC utility

  • gacutil-u MyLibrary

To uninstall a specific assembly using GAC utility

  • gacutil-u MyLibrary, Version=1.0.0.0, PublicKeyToken= ffrrty54d3432129

 

DLL Hell Problem


DLL Hell:

Application - AP1
Application - AP2
Shared Assembly

  1. I have 2 applications, AP1 and AP2 installed on my computer.
  2. Both of these applications use shared assembly
  3. Now, I have a latest version of Application - AP2 available on the internet.
  4. I download the latest version of AP2 and install it on my machine.
  5. This new installation has over written Shared.dll, which is also used by Application-AP1
  6. Application-AP2 works fine, but A1 fails to work, because the newly installed Shared Assembly is not backward compatible. 

So, DLL HELL is a problem where one application will install a new version of the shared component that is not backward compatible with the version already on the machine, causing all the other existing applications that rely on the shared component to break. With .NET strong named assemblies we don't have DLL HELL problem any more.


DLL Hell Resolution:

There are four steps for dll resolution.

  1. Figures out what version is required : Generally dependant assembly information is present in application's assembly manifest. CLR checks the application configuration file and machine config file for information that overrides the version information stored in the calling assembly's manifest.
  2.  Searches in GAC for assembly : .Net searches assembly in GAC only if assembly is strong named.
  3. If assembly not found in GAC, than there is a config file, then .Net searches  the location in the configuration file, else .Net searches directory contains .exe.
  4. If the assembly not found, the application terminates with error.

Version checking is not done for weak named assembly.

Previous
Next Post »