Adding assembly to GAC

Creating libraries of code for ASP.NET is great. It makes it very easy for code re-use. You can generate a library of common functions and compile them into a library. Although, this cuts down on a lot of duplicate code, anyone with a lot of different web sites runs into the problem of having to copy this library over and over. Then you lose track of which web application has which version of your library, and some of your libraries have added functions and some don't. Might get a little confusing.
Wouldn't it be nice if we could add our library into some common location likes and then just be able to access them by a simple using statment? Well, today, I'll show you how to add an assembly (your library) to the Global Assembly Cache (GAC) so that all your web applications (and windows applications) can access a library which is stored the central location.

Creating an assembly key file
The steps involved in adding an assembly to the GAC is not as simple as adding the assembly to a web application. Since all applications can access this library, some security precautions have been taken by Microsoft to ensure uniqueness, version protection, and code integrity. This is achieved by creating a strong name for your new assembly. A Strong Name consists of the assembly indentity and also a public key and digital signature.
To create a strong name, you use the syntax:


 
 
sn -k StrongNameFile.snk


(If your path environment variables isn't set properly, 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 anything you want. The snk extention is used by convention.

Write/Edit your assembly
We now have our strong name for our assembly, now it's your turn to write the assembly. Your assembly can be anything you like but to add this assembly to your GAC, you need some special syntax. In front of your namespace line, you have to put a tag which will link your assembly key to this assembly. This is achieve like this:
In C#:


 
 
using System.Reflection;
[assembly:AssemblyKeyFile("StrongNameFile.snk")]

namespace Devhood
//... rest of your code goes here ...


In VB.NET:


 
 
Imports System.Reflection
<assembly:AssemblyKeyFile("StrongNameFile.snk")>

Namespace Devhood
'... rest of your code goes here ...


Adding your assembly to your GAC
We're almost done. After you've created your strong name and your assembly, we just have to compile it and add it to your GAC.
Compile your assembly like you normally would with: "csc /t:library AssemblyFileName.cs". To add your assembly to the GAC use:


 
 
gacutil /i AssemblyFileName.dll


where AssemblyFileName.dll is the filename of the compiled assembly that you just compiled.

If you want to later uninstall this assembly, simply use the command:


 
 
gacutil /u AssemblyFileName


(Notice that this is just the assembly's name. No extension.

Adding your assembly machine.config
We reach our final step and we shift our attention to the machine.config file. The machine.config file is the configuration file for all the web applications on the server. It is located at: C:/WINDOWS/Microsoft.NET/Framework/v1.0.2914/CONFIG/machine.config. It is similar in to the web.config file but the web.config only works for the web application which it resides in.

Located the <assemblies> tag (which should be under <configuration>/<system.web>/<compilation>/<compilers>/<assemblies>)
Between the <assemblies> tag, you will enter:


 
 
<add assembly="AssemblyFileName, Version=0.0.0.0, Culture=neutral, PublicKeyToken=5edf592a9c40680c" />


To get the information that goes into the assembly attribute, you can run 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 (minus the Custom=null part at the end).

We now come to the conclusion of this tutorial. At this point, you will be able to place this line in your aspx pages.

 
 
      
      
 <%@Import Namespace="Devhood"%>


(or whatever namespace you declared in your assembly). Any web application which has an ASPX page with that import line will be able to access the classes of that namespace.

Tips

  • if you path environment variable is not set properly, you might have problems running sn.exe and gacutil.exe
  • make sure you add the System.Reflection namespace or else your assembly:AssemblyKeyFile tag will cause errors when compiling
  • if you have problems finding the assembly in the gacutil /l list, you can do "gacutil /l > gac.txt" and it will put it all into a textfile, then you can use notepad's find function.
    Links
    Devhood ASP.NET Message Forum - Feel free to ask questions to me or other Devhood members.
    Devhood C# Message Forum - Feel free to ask questions to me or other Devhood members.
    http://www.devhood.com - Can't forget to mention this great resource. :-)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值