- Public or Private?
- What's In a Strong Name?
- What's a GAC?
- Summary
What's a GAC?
After an assembly has been compiled with a strong name, it can be placed in the Global Assembly Cache, or GAC for short. The GAC is a machine-wide storage area under the windowsdirectory\assembly directory that is analogous to how the system registry was used for COM components. By placing an assembly in the GAC, it can now be found in this common location by the common language runtime at runtime. To place an assembly in the GAC, you can use the Global Assembly Cache command-line utility (GACUtil.exe), a Windows shell extension that ships with the SDK and can be used by opening the Windows Explorer to the assembly directory, or you can use the Microsoft .NET Framework Configuration utility installed in the Administrative Tools group. In any case, the GAC can store multiple versions of the same assembly and therefore allow different applications on the same machine to bind to different versions of the assembly.
As a result, if you reference a shared assembly in a VS .NET project, the Strong Name property in the IDE will show True for the assembly.
NOTE
In this version of VS .NET, you cannot reference an assembly directly from the GAC, so you need to reference it from another location on your machine.
At runtime, the common language runtime first checks the GAC for the particular strong name (compiled into the application as a reference) that was referenced. If not found, it will look in the application's relative path in the same way as for a private assembly. This precedence ensures that applications that use shared assemblies always look in the GAC first so that updates need only be made to the GAC.
Unfortunately, this precedence is also somewhat confusing because the VS .NET IDE shows the originally referenced path to the shared assembly in its Path property. However, at runtime, the GAC is consulted first. To see from where an assembly is loaded, you can also use the following snippet (VB) in your application to print out the location of the loaded assemblies and whether they were loaded from the GAC:
Imports System.Reflection Dim o As [Assembly] Dim a As [Assembly]() a = AppDomain.CurrentDomain.GetAssemblies() For Each o In a Console.WriteLine(o.FullName & " in gac = " & _ o.GlobalAssemblyCache & " location=" & o.Location) Next
The final issue that developers find confusing when using the GAC is the fact that the Add References dialog box does not show all the assemblies that are in the GAC. To add your own assemblies to the list of assemblies shown in the dialog box, you must add a key to the registry that VS .NET checks when the dialog box is invoked. This key can be found at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders
You simply need to add your own key (of any name) under this key, and in the default value, place the path to your shared assemblies. A good strategy is to place all of your shared assemblies in a common directory and then create this entry so that you can reference them easily from VS .NET.