C Sharp

Working with the Global Assembly Cache

Every .NET installation has a code cache called the global assembly cache. This area serves three primary purposes: -

  • It is used to store code downloaded from the Internet or other servers (both http and file servers). Note that code downloaded for a particular application is stored in the private portion of the cache-this prevents it from being accessed by others.
  • It is a data store for components shared by multiple .NET applications. Assemblies that are installed into the cache by using the Global Assembly Cache tool are stored in the global portion of the cache and are accessible by all applications on the machine.
  • One question I hear frequently is, "Where does the jitted code get stored such that my C# code is only jitted the first time it is executed?" Now you know the answer: native code versions of assemblies that have been prejitted are stored in the cache.

Viewing the Cache

Let's take a look at the cache to see the currently installed and shared assemblies. Using Microsoft Explorer, open the c:\winnt\assembly folder. To aid in viewing pertinent information about assemblies, .NET features a shell extension called the Assembly Cache Viewer (shfusion.dll). This tool enables you to view assembly information, such as the version number, culture, public key token, and even whether the assembly has been prejitted.

Another means of viewing the cache is by using the Global Assembly Cache tool. This tool enables you to perform several basic tasks by specifying any of the following (mutually exclusive) command-line switches.

  • -i This flag installs an assembly to the global assembly cache. An example would be the following:
  •  gacutil -i HelloWorld.DLL

    Shortly, you'll see how to add the Module3Client assembly to the cache by using this switch.

  • -u This flag uninstalls an assembly, including any version information, from the global assembly cache. If you don't specify the version information, all assemblies with the specified name are removed. Therefore, the first example here uninstalls all HelloWorld assemblies regardless of version number, and the second example uninstalls the specified version:
  •  gacutil -u HelloWorld gacutil -u HelloWorld, ver=1,0,0,0
  • -l This flag lists the contents of the global assembly cache, including the assembly name, its version number, its location, and its shared name.
In some of the earlier .NET betas, one problem I noticed was that when exploring the c:\winnt\assembly folder, the shell extension did not execute. This was caused by the fact that the shfusion.dll extension did not register properly. If this happens on your system, open a command prompt and enter the following from the c:\winnt\Microsoft.net\framework\v 🗙🗙🗙 folder, where 🗙🗙🗙 represents the version number of the .NET Framework that you're running. Obviously, because I'm working with a beta, the folder name will change before .NET ships. Search for the shfusion.dll file, and use that folder. Here I've used the folder representing my current .NET build:
c:\winnt\microsoft.net\framework\v1.0.2615>regsvr32 shfusion.dll

Now that you've created a public key file and assigned it to an assembly, let's add that assembly to the cache. To do that, type the following at the command prompt: -

gacutil -i Module3ClientApp.exe

If all goes well, you should receive the following confirmation: -

Assembly successfully added to the cache

At this point, you can use the gacutil -l command to view the assemblies listed in the cache and find the Module3Client, or you can use the Assembly Cache Viewer. Let's use the latter. If you open the cache in Windows Explorer (C:\Winnt\Assembly or C:\Windows\Assembly), you should now see the Module3Client assembly listed along with the other assemblies. Right-click that and select Properties, and you'll see things such as the public key value, version number, and physical location of the assembly on your hard disk. One point of reference is that your public key will be different than mine, but the main point is that it will be the same as that displayed by executing the Module3ClientApp application.