Using the Global Assembly Cache

Libraries which are regularly accessed by more than 1 website should be placed in the Global Assembly Cache (GAC) for improved efficiency.

Where is the GAC folder located in Windows?

C:\WINDOWS\assembly

Where is the gacutil.exe tool?

Framework 1.1 (Visual Studio 2003) : C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322

Framework 2.0 (Visual Studio 2005) : C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin

Framwork 3.5 (Visual Studio 2008) : C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

How to add an assembly (dll file) to the GAC?

See : http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=106

How to reference an assembly which is already in the GAC in your projects?

If you have a dll which you have put in the GAC and want to reference it in your web projects, you can add the assembly in your web.config file – as simple as this! So what you need to do is the following:

1. If your dll is called, ClassLibraryTest.dll, register it in the GAC with the gacutil.exe

2. At the command prompt (cmd), type gacutil -lr ClassLibraryTest > C:\tempGACFile.txt

3. From the contents of tempGACFile.txt you will see “The Global Assembly Cache contains the following assemblies:”. You need the line after this phrase to continue.

4. Open your web.config and stick this in as follows:

 

20 <compilation>

21 <assemblies>

22 <add assembly=ClassLibraryTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab75ca89730a76d7, processorArchitecture=MSIL/>

23 </assemblies>

24 </compilation>

5. Once this is done, your dll is already being referenced in your project. So all you have to do is use it in your pages.

 

11 // from the GAC

12 using ClassLibraryTest;

6. The rest you should know 🙂

If that does not work, then what you’ll need to do is create a copy of the dll on your drive and you will then have the option to browse to this dll to add a reference to this through Visual Studio. What will happen then is when you compile your application, the runtime will check whether you have this dll in your GAC and if you do then it will use the one from the GAC rather than the local copy which you have referenced in your project.

You can check that the dll from the GAC is the one which is being referenced rather than the local copy by looking at the bin folder for your project – you shouldn’t see the dll you are referencing in the bin!

comments powered by Disqus