Introduction
Each machine on which the Common Language Runtime is installed has a machine-wide code cache. This cache is divided into two components, a download cache and a global assembly cache.
The global assembly cache is used to store assemblies meant to be used by several applications on the machine. The only way to deploy an assembly into the global assembly cache is by using an installer designed to work with the global assembly cache, or a tool in the SDK such as al.exe.
There are several reasons you would want to install an assembly into the global assembly cache. These include:
·Performance improvements – Performance is improved in several ways for assemblies in the global assembly cache. First, an assembly does not have to go through verification each time it is loaded. Second, if multiple applications are referencing the assembly, the operating system will single instance the assembly and loading speed will significantly increase. Finally, the runtime can find the assemblies faster.
·Integrity checking – When an assembly is added to the global assembly cache, integrity checks are performed on all files of the assembly based on the hash value and hash algorithm specified for that file in the assembly manifest.
·File security – Only users with Administrator priviledges can delete files from the global assembly cache.
·Versioning – Multiple copies of assemblies with the same name but different version information can be maintained in the global assembly cache.
·Automatic pickup of QFEs – When the runtime locates an assembly through probing or through the codebase, it will check the global assembly cache for any QFEs of the assembly (providing Automatic QFE policy has not been turned off in the configuration file). If there is a version of the assembly in the cache that has a higher build and/or revision number, this assembly is loaded instead of the one found by probing or examining the codebase.
·Additional search location – If the runtime does not find an assembly via probing or through the codebase, it will check the global assembly cache for an assembly that matches the assembly request.
Assemblies deployed in the global assembly cache must have a shared name. When an assembly is added to the global assembly cache, integrity checks are performed on all the files that make up the assembly. Since the assemblies in the global assembly cache can be accessed directly from the file system, the cache performs these integrity checks to ensure that an assembly has not been tampered with (e.g. a file changed but the manifest version stayed the same). Only users with Administrator privileges on the machine on which the cache resides can perform management tasks on the global assembly cache such as installing or uninstalling assemblies.
How to create Shared Assembly?
The steps involved in creating shared assemblies is as follows
·Create your DLL/EXE source code
·Generate unique assembly name using SN utility
·Sign your DLL/EXE with the private key by modifying AssemblyInfo file
·Compile your DLL/EXE
·Place the resultant DLL/EXE in global assembly cache using AL utility
Creating unique assembly name
Microsoft now uses a public-private key pair to uniquely identify an assembly. These keys are generated using a utility called SN.exe (SN stands for shared name). The most common syntax of is :
sn -k mykeyfile.key
Where k represents that we want to generate a key and the file name followed is the file in which the keys will be stored.
Signing dll/exe
Before placing the assembly into shared cache you need to sign it using the keys we just generated. You mention the signing information in a special file called AssemblyInfo. Open the file from VS.NET solution explorer and change it to include following lines :
[assembly:AssemblyKeyFile("file_path")]
Now recompile the project and the assembly will be signed for you.
Place the assembly in shared cache
Microsoft has provided a utility called AL.exe to actually place your assembly in shared cache.
AL /i:my_dll.dll
Now your dll will be placed at proper location by the utility.
otherway of placing the assemblies in the shared cache, we just need to drag and drop the dll into the global assembly cache lies in file system as “C:\\Windows\assembly”.