Step by Step Procedure of How to Install an Assembly: Part I

Introduction

 
It is an assembly container provided by the .NET Framework especially to store assemblies shared and used by different .Net applications installed on a given computer or on a given network. It is located in a subdirectory of the root system but it can not be directly accessed except that administrator permission is given before. The GAC is accessed via the .NET configuration management console witch is represented below:
 
1.gif
 
Figure 1
 
Assume that an assembly is simultaneously used and/or reused by more than one developer or shared by more than one application. So, it should be shared by them all. Therefore using GAC is a good alternative. It is possible to install the assembly in the GAC and give developers privileges access to the system root rather than to copy it into each development station. It is possible that the assembly cache contains more than one version of the same assembly but it is important to mention in this context that assemblies' names don't have an extension like *.dll or *.exe when are displayed within the GAC list. Only the assembly name appears without any extensions because when the common language runtime CLR looks for a targeted assembly, it can precise the corresponding assembly according to the application needs. 
 
When an application is developed against an assembly, it depends on it, therefore it needs to locate it and exploit its services when running. First of all the common language runtime CLR, which is responsible for executing the application program, verifies if the assembly is already referenced and used, if this is not the case, it searches it, at the second plan, in the application bin directory, after that, it is checked in the global assembly cache GAC, in a final step, the CLR checks information about the targeted assembly in the configuration file, in this step the code base located in the configuration file gives information about the targeted assembly. When the assembly is not or can not be found, an error occurs. And a message like this can be found in the error list:
 
2.gif
 
Figure 2
 
Where can one find the Global Assembly Cache:
 
To obtain the assemblies' list in the Global assembly cache, follow this path:
 
Click Start --> Configuration Panel --> Administration tools --> .Net Framework Configuration 
 
The .NET Framework 2.0 Configuration management console is opened as bellow:
 
11111.gif
 
Figure 3
 
Expand the My Computer node within the tree just at the right and then select the assembly cache node then click view List of assemblies in the assembly cache hyperlink.
 
4.gif
 
Figure 4
 
The assembly list is displayed as bellow:
 
5.gif
 
Figure 5
 
How can one install an assembly in the GAC:
 
First of all, let us develop a simple assembly. To do that, follow those steps
  • Create a new class library project and name it myAssembly for example.
6.gif
6.gif
 
Figure 6
  • Add this code to the editor.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Windows.Forms;  
  5.    
  6. namespace myAssembly  
  7. {  
  8.     public class Class1  
  9.     {  
  10.         public Class1()  
  11.             {  
  12.                 MessageBox.Show("You are using myAssembly");  
  13.             }  
  14.     }  
  15. }  
  • Save the project.
     
    The GAC accepts only assemblies with strong names; therefore it is imperatively recommended to sign the assembly before adding it into GAC, otherwise, when adding a none strongly assembly, a message indicates that the new assembly can not be added to the GAC appears and finally the action is failed. So, to sign myAssembly, go to myAssembly properties as shown below.
7.gif
 
Figure 7
  • Select the signing tab and then check Sign the assembly checkbox.
8.gif
 
Figure 8
  • Select New in the combo box as shown above. This dialog box appears
9.gif
 
Figure 9
  • Enter the key file name with twelve characters, then enter a password with more than six characters and confirm it, then click Ok.
     
    A file looks like this small.gif is added to the application directory with *.snk as an extension which is a Strong Name Key abbreviation. This file contains random pair keys and it is provided to sign the assembly. This file can be generated also using the Strong name tool sn.exe provided by the .NET framework.
  • Build the solution, save the project and close it.
  • Switch to the .Net Framework management console and right-click on the Assembly cache node, a context menu appears.
  • Click add menu item, the open dialog box appears. 
10.gif
 
Figure 10
  • Browse to the assembly location and then click open. The assembly will be added to the global assembly cache without any problems.


Similar Articles