Deploy assembly to GAC step by step

 

Inroduction

This article covers complete understanding of global assembly cache (GAC),methods/steps to deploy an assembly in GAC, what are Strong name assemblies, steps to give strong name to an assembly and finally a sample code example of Global Assembly Cache using C-sharp code.

What is Global Assembly Cache (GAC) ?

Global Assembly Cache (GAC) is located on a system and stores assemeblies (DLLs) which is specifically will be shared by several applications (like web/windows). Common language runtime is installed has a machine code cache called the global assembly cache. 

In simple words it is a place where we can deploy common DLLs in GAC and that common DLLs which can be further be shared among several/multiple applications on the same computer.

Why we need GAC ?

Let's suppose we have multiple applications on a same computer (App1 & App2) and both of these applications using the same DLL or assembly in their BIN directory. Because both applications are using the same DLL (i.e. common DLL) on the same machine so we can now move this common DLL to a common place (global assembly cache - GAC) where both of applications (App1 & App2) can share this common DLL on the same machine.

How to deploy an assembly in GAC

In order to deploy an assembly in GAC, we have to create a strong name for an assembly.

Kindly Note : We cannot deploy an assembly in GAC without a strong name, Each and every assembly need to have a strong name for an assembly in order to deploy it in GAC.

So inorder to deploy or to install an assembly in GAC we need to have strong name for an assembly. So let's understand about strong names and how to give strong or how to create strong name keyfile file.

What are Strong name assemblies.

Strong name for an assembly guarantees the uniqueness by unqiue key pairs. Because an assembly generated with a strong name consist of unique identity with its public key, version number, text name, culture information and a digital signature.
 
It guarantees that no one can generate the same assembly with same private key and protects an assembly from versioning lineage and tampering. On passing to .NET framework it provides strong integrity check or security checks and guarantees that the contents of an assembly have not been changed since it was built.

Steps to give strong name to an assembly/DLL

Method 1 : Using Visual Studio Command Prompt
 
1. Open up the visual studio command prompt or .Net command prompt. (You can find command prompt in program files -> Visual studio -> Visual studio tools).
 
2.Click to open command prompt then navigate to project folder
 
eg: to navigate to any drive type drive name followed by colon (i.e. to navigate "D" drive type * D: and press on Enter button*). After navigating to a drive type "cd followed by any folder name" (i.e. cd Gacdemo and press on  Enter button).
 
Same way you can navigate to your own custom folder by using commands.
 
3.Go to the project folder using commands in visual studio command prompt
 
4.Once you are inside your project folder, Now let's create a strong name file by using following command as shown below.
 
sn -k Gacdemo.snk
 
You can replace "Gacdemo" with your own project name or any name as per your requirement.
 
Type "sn -k Gacdemo.snk" then press on Enter button.This will create  Gacdemo.snk (Strong Name Key File) in that folder.
 
Strong name file consist of unique identity or private key with its version number, text name, public key, culture information and a digital signature.

Method 2 : Using Visual Studio IDE 

First create any project in your visual studio, Once your project is been created.
 
GoTo-->Project(From Top Menu of Visual Studio)-->ProjectName Properties as shown in below image.

Then go to-->Signing Tab-->Sign the assembly-->Give new strong name or select existing strong name.

After giving strong name to a DLL, Now its time to deploy an assembly (DLL) into GAC. There are various methods from which you can deploy an assembly in GAC as shown below.

How to install or register an assembly in GAC

1st Method
 
Just drag and drop/ CopyPaste an assembly to an assembly folder.
 
To find an assembly folder first you need to check your windows installation drive, In my case my windows is installed in "C-drive" so its "C" drive C:\WINDOWS\assembly.

2nd Method
 
Using Microsoft .NET Framework SDK v2.0/V.4.0
 
Make sure that you installed .NET Framework SDK v2.0/V.40 in your mechine. If not Please download from the below given url and install it.
 
http://www.microsoft.com/en-us/download/details.aspx?id=19988 or http://www.microsoft.com/en-us/download/details.aspx?id=8279
 
1. Click Start > All Programs > Administrative Tools > Microsoft .NET Framework 2.0/4.0 Configuration.
2. Click Manage the Assembly Cache.
3. Select Add an Assembly to the Assembly Cache.
4. Browse and select your DLL, which you want to install it in GAC
5. Click Open. This will deploy the selected assemblies into the Global Assembly Cache (GAC).
6. Restart the IIS.

3rd Method
 
Open up visual studio command prompt not the windows command prompt.

Then navigate to your assembly drive type drive name and followed by colon i.e. d: 

Then command "cd" followed by folder name i.e. cd assemblyfolder

GAC Command

Now in order install/deploy an assembly in GAC using command prompt we need to use the following command as shown below
 
gacutil -i assemblyname

So now that you have understood about GAC, strong names and how to give strong name to an assembly and steps to register an assembly in GAC now its time to see a simple code example of GAC.

Sample code example of Global Assembly Cache

Here in this code example, we will demostrate step by step from DLL creation and deployment of a DLL in GAC and how to utilize shared DLL (Registered DLL) in windows form application.

Step 1 - Create a C-Sharp Project
 
Let's create a simple C# project
 
Step 2 - Code a project
 
Let's add some coding to it

 
   public class CommonGac
    {
        public string MachineName()
        {
            return "My Machine Name is : " + Environment.MachineName;
        }
    }

Step 3  - Give a strong name
 
GoTo-->Project(From Top Menu of Visual Studio)-->ProjectName Properties as shown in below image.

Then go to-->Signing Tab-->Sign the assembly-->Give new strong name.

We have created a new strong name as "CommonGacKey". Now lets save the project and build the project once again.

As you see friends a strong name key file "CommonGacKey.snk" is been added to our project folder.

Step 4 - Create a new common folder
 
In this step we will create a new common folder in the same drive or any other drive.
 
We have created a new folder called "GacCommon" in E-drive.

Step 5 - Move the DLL to common folder
 
In this step we will move the "CommonGac" project DLL to a new folder "GacCommon" which we have created in E-drive.
 
Step 6 - Deploy DLL in GAC
 
To deploy an assembly or DLL in global assembly cache we will use our visual studio command prompt and our GAC command.
 
i.e. gacutil -i assemblyname
 
gacutil -i CommonGac.dll

As you see from our above snapshot that we have successfully added "CommonGac.dll" to GAC which was stored in E-drive GacCommon folder.

Step 7 - Add new project
 
Now we have to add a new project "Windows Application or Web Application" to our existing C# project. In this scenario we will add "Windows Application".

 

Step 8 - Code the Windows Application Project
 
First we will add a simple button on Form screen then we will reference the "CommonGac.dll" which we have saved in new folder "GacCommon" in E-Drive (because DLL present in "GacCommon" folder is got registered in GAC).

As you see in our above image snapshot of solution explorer that we have referenced "CommonGac.dll" to our Windows Application Project.

Step 9 - Call the Class in Button Code
 
In this step we have to import the namespace "CommonGac" and call the "CommonGac" class object in the button code as shown in the below snippet of code.

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CommonGac;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        CommonGac.CommonGac objGac; 

        private void button1_Click(object sender, EventArgs e)
        {
            objGac = new CommonGac.CommonGac();

            MessageBox.Show(objGac.MachineName());

        }
    }
}

Finally build the windows application solution .
 
Step 10 - Copy the EXE to Any New Folder
 
After building the windows application solution an exe file will be created inside bin\debug directory.
 
Copy that exe "WindowsFormsApplication1.exe" to any folder or any drive. We have copied the "WindowsFormsApplication1.exe" to that common folder "GacCommon" in E-drive.
 
Step 11 - Finally Output
 
Double click on "WindowsFormsApplication1.exe" to check the output. Let's check the output.

Hey friends hope you understood about global assembly cache and steps to deploy an assembly into gac. In the further upcoming article we will also cover how to handle multiple versioning of DLL in GAC.

If you have any doubts kindly let me know through your comments and kindly share this article with your friends on google+, facebook and twitter etc. Thank you

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Mar
14
2014

Understanding concept of Concat,Substring methods of a string class ?

CLICK TO READ FULL ARTICLE

Views - 4396 |Category - C#

Mar
10
2014

C# if and nested if conditional statements with an example?

CLICK TO READ FULL ARTICLE

Views - 10507 |Category - C#

Mar
08
2014

Practical - Count repeated words in a string using Regular Expression ?

CLICK TO READ FULL ARTICLE

Views - 5507 |Category - C#

Dec
07
2013

What are generic collections in C# using an example?

CLICK TO READ FULL ARTICLE

Views - 32929 |Category - C#

Dec
02
2013

What are generics in C# using an example?

CLICK TO READ FULL ARTICLE

Views - 34477 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

alt09

 

 
Comments

Posted by Shawpnendu on 2014-07-27

Nice explanation thanks. REGARDS shawpnendu