Back to: C#.NET Tutorials For Beginners and Professionals
How to Install an Assembly into GAC in .NET Framework
In this article, I am going to discuss How to Install an Assembly into GAC in .NET Framework with Examples. Please read our previous article where we discussed Strong and Weak Assemblies in .NET Framework.
What is GAC?
GAC stands for Global Assembly Cache and it contains only the Strong Named Assemblies. Each computer where the Common Language Runtime is installed has a machine-wide code cache called the Global Assembly Cache. An assembly is said to be strongly named when it contains the following properties
- The Assembly Name.
- Version Number.
- The Assembly should have been signed with the Private/Public Key Pair.
The Global Assembly Cache stores assemblies specifically designated to be shared by several applications on the computer. It is recommended to install an assembly into GAC, only when required and shared by applications, otherwise, they should be kept private.
Starting with .NET Framework 4, the default location for the Global Assembly Cache (GAC) is C:\Windows\Microsoft.NET\assembly. In earlier versions of the .NET Framework, the default location is C:\Windows\Assembly.
How to Install an Assembly into GAC in .NET Framework?
To install an assembly into the GAC, the assembly must be strongly named, otherwise, you will 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.
- Simply Drag and Drop
- Use GacUtil.exe (GAC Utility Tool)
Creating a Strongly Named Assembly in .NET Framework:
First, create a class library project with the name SampleAssembly and then rename the Class1.cs class file with Calculator.cs class file and then copy and paste the following code into it.
namespace SampleAssembly { public class Calculator { public int Calculate(int x, int y) { return x + y; } } }
When you compile the above project, it will create an assembly with the name SampleAssembly.dll and this is not a strong name assembly. Let us try to add the SampleAssembly.dll into GAC and see what happens. The Physical Path of our Assembly is
D:\Projects\AssemblyDemo\SampleAssembly\bin\Debug\SampleAssembly.dll
Next, open Visual Studio Command Prompt in Administrator mode and then copy and paste the following command and press the enter button. Here, -i is for installation, similarly, if you want to uninstall an assembly, then you need to use -u.
Gacutil -i D:\Projects\AssemblyDemo\SampleAssembly\bin\Debug\SampleAssembly.dll
Once you type the above command and press the enter button, then you will see the following error saying Failure adding assembly to the cache: Attempt to install an assembly without a strong name.
This is because our assembly is not a strong name assembly and it is not signed with any private public key. To prove this, create a console application and then copy and paste the following code into it. You need to replace the path as per your SampleAssembly.
using System; using System.Reflection; namespace AssemblyDemo { class Program { static void Main(string[] args) { //Replace D:\Projects\AssemblyDemo\SampleAssembly\bin\Debug\SampleAssembly.dll //With your Assembly Path Console.WriteLine(Assembly.LoadFile(@"D:\Projects\AssemblyDemo\SampleAssembly\bin\Debug\SampleAssembly.dll")); Console.ReadKey(); } } }
Now, run the application and you should see that the PublicKeyToken value is null as shown in the below image.
How to Make the Assembly a Strong Name Assembly?
To make our assembly strongly named we need to sign our assembly with a Private and Public Key Pair and we have discussed this in our previous article.
In the .NET Framework, we have a tool called Strong Naming Tool (sn.exe) and we can use this (sn.exe) tool to generate the Private Public Key pair. Again, in order to use this tool, we need to use the Developer Command Prompt for Visual Studio. So, open Developer Command Prompt for Visual Studio in Administrator mode and then type sn.exe -k D:\MyKeyFileForSampleAssembly.snk and press enter button as shown in the below image.
Once you type the required command and press enter, the key file with the name MyKeyFileForSampleAssembly.snk should be generated in the D: Drive
Once you generated the Key file, then you need to use the AssemblyKeyFile attribute in the AssemblyInfo class to sign the assembly with a strong name. So, open the AssemblyInfo class file of the SampleAssembly project, and then to the constructor of the AssemblyKeyFile attribute, we need to pass the path of the key file that contains the private and public keys as shown below.
[assembly: AssemblyKeyFile(@”D:\MyKeyFileForSampleAssembly.snk”)]
Once you add the above AssemblyKeyFile attribute, build the solution. Once you build the solution, now your assembly sign with a private-public key pair. Now, our assembly contains the Public Key Token. To see the assembly information, please execute the Console Application again and you should see the following output.
Now, you can see, our assembly contains the name, version number, and public key token which are required for a strongly named assembly. Now, let us try to install the SampleAssembly.dll to GAC. So, open Visual Studio Developer Command Prompt in Administrator mode and then type Gacutil -i D:\Projects\AssemblyDemo\SampleAssembly\bin\Debug\SampleAssembly.dll command and press the enter button as shown in the below image.
Here, you can see, we are getting the message saying Assembly successfully added to the cache. You can find the assembly in the following location.
C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\SampleAssembly\v4.0_1.0.0.0__8f8fc05537931ee7
How to use the SampleAssembly in other Projects?
Let us assume we want to consume Calculate method of the Calculator class from our console application. But, here, we are not going to add the reference to the Class Library project, rather we will add the reference to the Assembly from the GAC.
First, we need to add a reference to the SampleAssembly.dll from the location (C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\SampleAssembly\v4.0_1.0.0.0__8f8fc05537931ee7) where it is stored in GAC as shown in the below image from our console application.
Now, modify the Main method of the Program class of our Console Application as shown below.
using SampleAssembly; using System; namespace AssemblyDemo { class Program { static void Main(string[] args) { Calculator calculator = new Calculator(); Console.WriteLine(calculator.Calculate(100, 200)); Console.ReadKey(); } } }
Now, run the application and you will get the output as expected.
How to Uninstall an Assembly from GAC in .NET Framework?
To uninstall an assembly from the GAC, using the GAC utility, we need to use the following command.
Gacutil -u SampleAssembly
If there are multiple versions of SampleAssemblyin the GAC, then all those 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 SampleAssembly,Version=1.0.0.0,PublicKeyToken=8f8fc05537931ee7
So, open Visual Studio Developer Command Prompt in Administrator mode and then type the gacutil -u SampleAssembly,Version=1.0.0.0,PublicKeyToken=8f8fc05537931ee7 command and press the enter button as shown in the below image.
In the next article, I am going to discuss DLL Hell Problems and How to Overcome DLL Hell Problems in .NET Framework with Examples. Here, in this article, I try to explain How to Install an Assembly into GAC in .NET Framework with Examples. I hope you enjoy this How to Install an Assembly into GAC in .NET Framework article.