Chapter 8 - Assemblies | |
bySimon Robinsonet al. | |
Wrox Press 2002 | |
The global assembly cache is, as the name implies, a cache for globally available assemblies. Most shared assemblies are installed inside this cache, but some private assemblies can also be found here. If a private assembly is compiled to native code using the native image generator, the compiled native code goes into this cache, too!
In this section, we shall explore:
Creating native images at installation time
Viewing shared assemblies with the Global Assembly Cache Viewer and the Global Assembly Cache Utility
With the native image generator Ngen.exe we can compile the IL code to native code at installation time. This way the program can start faster because the compilation during run time is no longer necessary. The ngen utility installs the native image in the native image cache, which is part of the global assembly cache.
Important | Creating native images with ngen only makes sense if native images are created for all assemblies used by the application. Otherwise the JIT compiler would have to be started anyway. |
With ngen myassembly , we can compile the MSIL code to native code, and install it into the native image cache. This should be done from an installation program if we would like to put the assembly in the native image cache.
Important | After compiling the assembly to native code you cannot delete the original assembly with the MSIL code because the metadata is still needed, and, if the security changes on the system, the native code will be rebuilt. |
With ngen we can also display all assemblies from the native image cache with the option /show . If we add an assembly name to the /show option we get the information about all installed versions of this assembly:
The global assembly cache can be displayed using shfusion.dll, which is a Windows shell extension to view and manipulate the contents of the cache. A Windows shell extension is a COM DLL that integrates with the Windows explorer. You just have to start the explorer and go to the < windir > /assembly directory.
The picture below shows the assembly cache viewer:
With the Assembly Viewer, the Global Assembly Name , Type , Version , Culture , and the Public Key Token can be seen. With the Type we can see if the assembly was installed using the native image generator. Using the context menu when selecting an assembly, it's possible to delete an assembly, and to view the properties:
The real files and directories behind the assembly cache can be seen by viewing the directory from the command line. Inside the < windir > \assembly directory there's a GAC and NativeImages_ < runtime version > directory. GAC is the directory for shared assemblies, and in NativeImages_ < runtime_version > we can find the assemblies compiled to native code. If you go deeper in the directory structure you find directories names that are similar to the assembly names , and below that a version directory and the assemblies themselves . This makes it possible that different versions of the same assembly can be installed.
The assembly viewer can be used to view and delete assemblies using the Windows explorer, but it's not possible to use it from scripting code, such as to create installation programs. gacutil.exe is a utility to install, uninstall, and list assemblies using the command line. Naturally, it can be used within scripting code for administration purposes.
Some of the gacutil options are:
gacutil /l lists all assemblies from the assembly cache
gacutil /i mydll installs the shared assembly mydll into the assembly cache
gacutil /u mydll uninstalls the assembly mydll
gacutil /ungen mydll uninstalls the assembly from the native image cache