Click here to Skip to main content
15,881,852 members
Articles / DevOps / Deployment
Tip/Trick

Installing a DLL into the Global Assembly Cache (GAC)

Rate me:
Please Sign up or sign in to vote.
4.65/5 (15 votes)
30 Apr 2013CPOL3 min read 175.8K   15   5
How to add a DLL to the GAC

Introduction 

This article provides a simple explanation of how to install a DLL into the Global Assembley Cache (GAC). 

There are several reasons you might want to do this: 

Sharing and Versioning the Assembly

The assembly may be used by more than one application and more than one version of the assembly may be required. Multiple versions can sit side-by-side in the GAC. 

Security 

The GAC is held in the system root so is likely to be more secure out-the-box. You also don't have to secure it multiple places. 

Assembly Searching

The GAC is checked after the local folder and before probing and checking the codebase information. 

Getting Started 

Create a strong name key pair:

Firstly we need to create a strong name key file. This will be in the format: sn –k <filename and location>. An example is shown below. This creates a file containing a public\private key pair.  

Image 1

Sign the assembly via Visual Studio

Signing the assembly is very easy - you could do it the hard way using an assembly linker but why would you? Right click on the project and go to properties. Select the signing tab then select the ‘sign the assembly’ check box. Point to the key pair file you created above. In this example I have copied the key file into the bin\debug folder and I am using it to sign a ReportingUtilities DLL which is generated in the same folder. 

Image 2

Installing into the GAC

Now that we have a signed assembly we can install it into the GAC. Run the Visual Studio command prompt (in visual studio tools) and write the following instruction in the format: gacutil –I <assembly name >. In this example I have changed the cmd prompt directory to the location of the DLL - you can do this or put the full assembly path in the assembly name.      

Image 3

Install into the GAC via an MSI

In a production environment the reality is that you won’t have visual studio installed.

Not to worry. To install a DLL into the GAC is still quite easy - you just need to create a deployment project.

In Visual Studio add a deployment project to your visual studio solution.

  • Choose a standard set up program – not wizard.
  • Right click on the project, select view, file system
  • Right click on the File System on target machine and select GAC

Image 4

  • Right click on the generated GAC folder and select the primary output from the DLL project as shown below. Make sure you select the correct project from the drop down!

Image 5

The project will also add a launch condition for the .NET framework. Make sure this version is set to the version that is going to be on the target machine as it will default to the latest version on your development machine (which might be greater).

To change this version:  

  • Right click on the project and select launch conditions
  • In the launch conditions select the .Net Framework
  • Click the properties tab. You may need to reselect the launch condition to highlight the correct properties. It should look like so:

Image 6

You can now build your project and copy the files to the environment where you need to install the DLL. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect http://www.uiguy.co.uk
United Kingdom United Kingdom
I am an enterprise application developer with around 8 years .Net experience.

I am currently focusing on Silverlight, WCF and SQL Server.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Member 1174065010-Jun-15 2:13
Member 1174065010-Jun-15 2:13 
QuestionInstalling in wrong gac Pin
Petros Orloff26-Jun-14 5:13
Petros Orloff26-Jun-14 5:13 
QuestionNice oNe Pin
Brijesh Kr25-Nov-13 5:20
Brijesh Kr25-Nov-13 5:20 
GeneralMy vote of 5 Pin
AbuOmar7-Oct-13 9:06
AbuOmar7-Oct-13 9:06 
Questionuse MSI Pin
adriancs30-Apr-13 23:36
mvaadriancs30-Apr-13 23:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.