AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=394&pId=-1
Installing an Assembly in GAC
page
by Haroon Malik
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 48489/ 53

Synopsis

To share assemblies within an ASP.NET web application they are required to be placed within the \bin directory, but in order to share them through different applications existing on the same web server it is required to install them into the Global Assembly Cache (GAC). This article will provide guidance to you in the process of adding an assembly to the GAC.

Introducing Global Assembly Cache

The \bin directory in an ASP.NET web application makes available the compiled components to all of the pages within the application.

Placing compiled components and assemblies into the \bin directory ensures the availability of the components and assemblies to all the pages that are contained in an ASP.NET web application. Any new assembly or compiled component when needed to be added to a web application is also placed within the \bin directory to mark it as available to the web application. The scope of the \bin directory is however limited to just the web application in which it is created. That means that if you have to share a single component or an assembly within two or more web applications, you would have to place a copy of it in each of the application's \bin directory.

The Global Assembly Cache in the .NET framework provides a central place for registering assemblies. These assemblies once registered are then available to all applications for usage, including the development environments like Visual Studio.NET and Web Matrix etc. The process of adding an assembly to the Global Assembly Cache can be compared to the process of registering COM components in the server, as is done in the case of classic ASP applications. However the process of adding an assembly or a compiled server component to the GAC is much easier as compared to the classic COM registering approach.

Creating a Strong Name

After creating an assembly and before registering it to the Global Assembly Cache the first thing that is required is to assign a strong name to the assembly. What is a strong name? Well! a strong name is basically assigned to an assembly or a component to distinguish it from other assemblies and components existing in the GAC. A strong name consists of an assembly identity (name, version, etc.), public key and a digital signature.

Assigning a strong name to an assembly ensures that it would have uniqueness, version protection and code integrity. Assigning a strong name to an assembly is not a difficult task. A utility known as the sn utility, can be used to generate strong names, which are then added to the code of the assembly. For example, to create a strong name for an assembly named as sample.dll, you would write on the command prompt.

sn -k sample.snk

This would generate a strong name key pair and store it in a file named as sample.snk. The extension of the file can be anything, but mostly .snk is used as a convention. The -k option here is for creating a strong name key pair. There are other options also available which you could search for in MSDN.

Associating a Strong Name with the Assembly

After generating the strong name key pair file, it is required to associate this file with our assembly, for doing that you have to add the following lines in the code of your assembly.

Imports System.Reflection

<assembly: AssemblyKeyFile("sample.snk")>

Namespace HRM.Samples

Note here that the information regarding the file containing the strong name key pair is placed in the code file before the namespace declaration. Also you are required to import the System.Reflection namespace in order for the statement to work, otherwise the compiler would be showing you an error stating that it does not recognize the <assembly: AssemblyKeyFile("sample.snk")> statement.

Adding Assembly to the GAC

After compiling the assembly with the statements, containing the strong name information being added to it, you now have to place the assembly into the GAC. You can either do it manually by simply copying and pasting the assembly into the GAC, which is located at;

c:\winnt\assembly

or you can also use a utility gacutil that is installed with the .NET framework. For adding an assembly you would write as follows on the command prompt.

gacutil /i sample.dll

Here the /i option is for installation. And for removing an assembly from the GAC, you can use the same utility as follows.

gacutil /u sample.dll

The /u option here is for uninstalling or removing an assembly from the cache.

Conclusion

In this article you learned the process of adding an assembly to the GAC. First you were introduced to what Global Assembly Cache is and how it differs from the \bin directory, by expanding the scope of the assemblies and compiled components by making them available throughout applications.

Next you were told about Strong Names and why assemblies are required to be strong named before they are to be added to the GAC. After generating a strong name key pair file, you saw how its get associated with the original assembly file and finally how you can add an assembly to the GAC directory.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 11:14:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search