A global assembly is a public assembly that is shared by multiple applications. Unlike private assembly, a global assembly is not copied to bin directory of each application that references it. Global assembly instead is placed in
GAC (Global Assembly Cache) and it can be referenced anywhere within the system. So only one copy is stored, but many applications can use that single copy.
In order to convert a private assembly to global assembly, we have to take the following steps.
- Create a strong name
- Associate strong name with assembly
- Place assembly in GAC
Creating a strong name
Any assembly that is to be placed in GAC, must have a strong name. Strong name is a combination of
public key and private key. The relationship between public and private keys are such, given one you cannot get the other, but any data that is encrypted with private key can be decrypted only with the corresponding public key.
Take the following steps to invoke SN (Strong Name) tool to create strong name.
- Go to command prompt using Microsoft .NET Framework SDK v2.0 -> SDK Command prompt
- Go to c:\csharp\counterlibrary folder and enter the following command.
sn -k srikanth.key
The above command writes private and public key pair into srikanth.key file.
Associate strong name with assembly
Once private and public keys are generated using SN tool, use the following procedure to sign
counterlibrary with the key file.
- Open counterlibrary project.
- Select project properties using Project -> counterlibrary properties
- Select Signing tab in project properties window
- Check Sign the assembly check box
- Select srikanth.key file using Choose a strong name key file combo box
- Close properties window
- Build the solution again using Build->Build Solution
Now,
counterlibrary.dll is associated with a public key and also digitally signed with private key. This ensures no one can modify this assembly as any change to assembly should re-sign the assembly with private key of the user who created it first. This protects the assembly from getting tampered with by others. A global assembly needs this projection as it is placed in common place.
You can verify whether the assembly is associated with public key using ILDASM (IL Disassembler) program provided by .NET Framework.
- Start ILDASM using .NET Framework SDK v2.0->Tools->MSIL Disassembler
- Select counterlibrary.dll using File->Open
- Once assembly is opened, double click on Manifest section of the assembly to see the public key associated with the assembly.
Place assembly in GAC
In order to make an assembly a global assembly, the assembly must be associated with a strong name and then placed in
Global
Assembly
Cache (GAC).
GAC is a folder with name Assembly in windows folder of your system. So, place counterlibrary.dll in GAC using GACUTIL tool as follows.
c:\csharp\counterlibrary\bin\Release>gacutil -i counterlibrary.dll
After you install global assembly into GAC, you can see counterlibrary.dll in windows/assembly folder.
Once, you place an assembly in GAC, any reference to the assembly will not create a copy of the assembly in BIN directory of the application. Instead all application that reference the assembly use the same copy that is placed in GAC.
http://srikanthtechnologies.com/articles/dotnet/globalassembly.html