What is GAC? with example

The Global Assembly Cache (GAC) is a repository for shared .NET assemblies in the Windows operating system. It allows multiple applications to access and use the same version of an assembly. Here's an example to illustrate the concept:

  • GAC stands for Global Assembly Cache.
  • GAC is the place where public assemblies are stored.
  • Private assembly can be converted into public assembly by adding them in GAC using gacutil tool.

Let's say you have two applications, App1 and App2, both of which require the same version of a shared assembly called "MyLibrary.dll". Instead of copying the "MyLibrary.dll" file into the application directories for both App1 and App2, you can install the assembly into the GAC and have both applications reference it from there.

Here's how you can install an assembly into the GAC using the Global Assembly Cache Tool (Gacutil.exe) from the command line:

  1. Open the command prompt as an administrator.
  2. Navigate to the directory where "MyLibrary.dll" is located.
  3. Run the following command to install the assembly into the GAC:
    bash
  1. gacutil /i MyLibrary.dll

This command installs the "MyLibrary.dll" assembly into the GAC, making it available for use by any application on the system. The assembly is now accessible by its strong name and can be referenced by applications that need it.

To reference the assembly in your application, you can specify its strong name or use its fully qualified name in the application's code or configuration files.

For example, in App1 and App2, you can add a reference to "MyLibrary.dll" by specifying its strong name or by using the fully qualified name:

csharp
// Strong name reference using MyLibrary; // Fully qualified name reference using MyLibrary = MyNamespace.MyLibrary;

By installing the shared assembly into the GAC, both App1 and App2 can reference and use the same version of "MyLibrary.dll" without needing a local copy in their respective application directories.

The GAC simplifies the deployment and maintenance of shared assemblies, ensuring that applications can access the required versions of shared components in a centralized manner. It provides a consistent and controlled environment for managing shared .NET assemblies on a system.

Post a Comment

Previous Post Next Post