Wednesday, June 3, 2009

Step by Step Process to add an Assembly file in GAC


GAC Stands for Global Assembly Cache. It is a kind of repository to store all the assemblies, also compatible to save different versions of same assembly.

If we add the assembly to GAC, then the assembly is also called as Shared Assembly.

The assembly available in GAC can serve multiple applications.

If we want to add our assembly to GAC, we to follow the steps listed here,

Step by Step Process to add an Assembly file in GAC

1) Create an assembly key file

Use the sn.exe tool to create a key file:
sn -k StrongNameFile.snk

“Assembly key file” is an identity to refer an assembly from GAC, because of this key file, we can store the multiple versions of same assembly.

If your path environment variables aren't set, you'll have to go to the C:\Program Files\Microsoft.NET\FrameworkSDK\Bin\ directory to run sn.exe)
The filename "StrongNameFile.snk" can be any name you want.

2) Edit your assembly
Now you have to add a tag which will link your assembly key to the assembly:

using System.Reflection;

[assembly: AssemblyKeyFile("StrongNameFile.snk")]

Normally this is done in the utility AssemblyInfo.cs (vb) file.

3) Add your assembly to the GAC by run the command thru cmd.exe

gacutil /i AssemblyFileName.dll

To uninstall this assembly from the GAC, use the command:

gacutil /u AssemblyFileName

4) (Opional) Add your assembly to machine.config

Locate the tag (for web apps,under ////)
Between the tags, enter:

You can get the information for the assembly attribute, by running the gacutil /l command which will return a list of all the assemblies in the GAC. You will have to look for the one you just added and copy the entire line (less the Custom=XXX part at the end).

At this point, you will be able to place this directive in your aspx pages.

Or, you can set a reference in the Visual Studio IDE as you would with other GAC Assemblies.

No comments:

Post a Comment