Downloads
Global Assembly Reference Aid
Introduction
It's not possible to add a reference to an assembly which is located in the Global Assembly Cache (GAC) via Visual Studio.NET. Most of the time this will not be a problem, but sometimes it is. For instance, when doing SharePoint development. If you want to use classes which are located in the Microsoft.SharePoint.Security namespace you will find them to be a part of the assembly Microsoft.SharePoint.Security.dll which is located in the GAC. The GAC Reference Aid tool assists in referencing assemblies located in the GAC.
Background information
Let's first discuss some topics related to the GAC. The GAC is the centralrepository for assemblies which are installed on a Windows machine (including all important .NET system assemblies implementing the Framework Class Library). By the way, all assemblies which are stored in the GAC must be strong named.
The primary purpose of the GAC is to eliminate problems of sharing DLL code. The GAC was originally called the Fusion cache and is currently implemented within Fusion.dll. To put it in other words, the GAC is controlled by the system-level component Fusion.dll. This is not all that Fusion.dll does, it's also responsible for maintaining other assembly caches: the download cache and the 'ZAP' cache. The download cache is where downloaded assemblies end up when they are executed from a URL. The 'ZAP' cache seems to serve as the home for pre-compiled native assembly images that are produced by the NGEN tool.
There are several ways to interact with the GAC, for instance via the gacutil tool or ShFusion.dll, which is a Windows Explorer shell extension that provides a user-friendly interface to the GAC. If you want to see ShFusion.dll in action go to the <%windir%>\assembly folder (see figure ShFusion ). You can also access the GAC through the Fusion API (implemented in Fusion.dll).
Adding references to assemblies in the GAC
Let's return to the original problem, referencing assemblies in the GAC. It's not possible to do that in VS.NET, but several workarounds are available.
Let's take the Microsoft.SharePoint.Security.dll as an example. If you want to reference it, you could manually copy the DLL out of the GAC and add the reference. In this case, open a command prompt and go to <%windows%>:\assembly\GAC\Microsoft.SharePoint.Security\11.0.0.0_71e9bce111e94 (Because of ShFusion.dll you can't do this via Windows explorer). After that you can copy the DLL to another location, like this: copy microsoft.sharepoint.security.dll [DriveLetter]:\mydestionation. Then you can use VS.NET to add the reference.
If you absolutely insist you could disable the ShFusion explorer shell extension by using the Registry Editor (regedit). If you add a binary value called 'DisableCacheViewer' to the registry key HKLM\Software\Microsoft\Fusion (see figure Disable cache view ) and set it to a non-zero value you disable ShFusion and you will be able to see the physical directory structure of the GAC (see figure Explorer view GAC ). Now you can copy DLL's in the GAC via Windows explorer.
By default, in VS.NET, you cannot reference assemblies in the GAC. If you look at the Add Reference dialog box however, you will see a list of assemblies. This is not an enumeration from the GAC, but it's possible to add paths to assemblies so that they will be displayed in this dialog box. MSDN article INFO: How to Display an Assembly in the Add Reference Dialog Box explains how to do this.
You can also use the simple (and free) tool GAC Reference Aid. This provides an easy way to copy assemblies located in the GAC in a user friendly way. One of the problems locating assemblies in the GAC has to do with the fact that the directory scheme, although it can be traversed, is used by Fusion.dll and is an implementation detail which will change as the CLR evolves. So the only viable option to build this tool was to interact with the Fusion API (see figure GAC Reference Aid ).
This tool uses the Microsoft.CLRAdmin.Fusion class which is located in the mscorcfg assembly. Specifically, we call the static ReadCache method to read the GAC. There's one small problem, however, this method is private. Normally you wouldn't be able to call this method, but if you use reflection to call this method you can. Via reflection you're able to invoke all members, even those that because of restrictive access modifiers are inaccessible to you (this does mean callers have to have the ReflectionPermission right, otherwise you will not be able to use the tool).
Disclaimer
We've created the tool to facilitate the job of referencing assemblies located in the GAC. Feel free to use the tool in any way you see fit, as long as you understand we don't take any responsibility in the (very) unlikely event something get's messed up.
Bugs
We've tested the tool and it works for us. If you use the tool and find a problem with it you can report it to news@lcbridge.nl . But we have to be honest here, if we don't have the time, we won't help you. However, if you want to have the source code of the tool, don't hesitate to ask, it's no secret.
Links
- Strong named assemblies.
- Creating strong named assemblies.
- How to display an assembly in the Add Reference dialog box
- INFO: How to Display an Assembly in the Add Reference Dialog Box
- Global Assembly Cache (GAC) API
Download
Download the GAC Reference Aid