First, we see what is a private assembly
By default, every assembly is a private assembly. If we add a reference to a private assembly to any project, a copy of the assembly is given to the project. So each project maintains a private copy of the assembly such as shown below.
Shared Assembly
A shared assembly is an assembly that resides in a centralized location known as the GAC (Global Assembly Cache) and that provides resources to multiple applications. If an assembly is shared then multiple copies will not be created even when used by multiple applications. The GAC folder is under the Windows folder.
<drive>:\windows\assembly <-- GAC folder
We can find all base class libraries under GAC.
We can only get a strong-named assembly into the GAC.
GAC contains multiple assemblies and it is identified by PUBLIC KEY TOKEN.
How to generate a public key token?
We have a tool named strong name utility to do this, which is a command-line tool and should be used from a command prompt as follows.
Sn -k <file name>
E.g
<drive>:\<folder> > sn -k key.snk
The above statement generates a key value and writes it into "key.snk".
We can use sn or snk for the extension of key files.
Creating A shared assembly
Step 1. Generate a key file. Open a VS command prompt. Go into your folder and generate a key file as
<drive>:\<folder> sn -k key.snk
Step 2. Create a project and associate a key file to it before compilation so that the generated assembly will be strongly named.
Open a new project of type class library and name its Assembly; under class1 write the following.
public string sayhello()
{
return "hello from shared assembly";
}
To associate a key file we generated with the project, open the project properties and select the "signing" tab on the LHS which displays a CheckBox as "sign the assembly" Select it that displays ComboBox below it is a select browse and select key.snk from its physical location then compile the project using build which will generate assembly Assembly.dll that is strongly named.
Step 3. Copy the assembly into GAC.
.Net provides a command-line utility to be used as shown in the following.
Gacutil -I | -u <assembly name> I:install u:uninstall
Open a VS command prompt; go to the location where the Assembly.dll is present and write the following.
<drive>:\<folder>\sAssembly\sAssembly\bin\Debug>gacutil -I Assembly.dll
Step 4. Testing.
Open a new project add a reference to Assembly.dll and write the following code for the button click event.
sAssembly.Class1 obj = new sAssembly.Class1();
MessageBox.Show(obj.sayhello());
Run the project and verify under the bin/debug folder of the current project where we will not find a copy of the Assembly.dll as it is a shared assembly.