How do I create shared assemblies ?
How to install an assembly in the Global Assembly Cache in Visual Basic .NET or in Visual Basic 2005?
A shared assembly
is one that is used by multiple applications on the machine. A shared assembly must have a name that is globally unique. The .NET Framework supports these naming requirements through a technique called strong names.
Shared assemblies must have a "strong name" consisting of the name of the assembly, the version, a 64 bit hash of a public key (called a ´token´) and the culture.
To create a strong name for an assembly (that is, to generate a public/private key pair), you'll have to use another utility called sn.exe.
In this article, we will create a Visual Basic 2005 component
called myGAC. We will also create a key file named mykeys.key. We will sign our component with this key file and place it in
Global Assembly Cache
To create a Component
we have to create a Class Library project
by using Visual Studio .NET.
Step 1 : Open Visual Studio .NET
Create a new Class Library project named myGAC in Visual Studio .NET or in Visual Basic 2005.
Here is the code for the component. It just includes one method which returns a string.
public class HelloClass
public function GetData() as string
return ´hello, how are you doing!´
Step 2 : Generating Cryptographic Key Pair using the tool SN.Exe
Now, we will use the SN tool to generate the cryptographic key pair, which is located in the \bin subdirectory, where the .NET Framework Solution Developer Kit (SDK) is installed. We will open the DOS prompt. The command-line statement format is:
sn -k "C:\[DirectoryToPlaceKey]\[KeyName].key"
Next, create a directory named mykeys in C:\ so that you can easily locate the key and access the key from the command prompt.
For most users, the .NET tools are located in C:\Program Files\Microsoft.NET\FrameworkSDK\Bin.
Type the following at the command prompt.
sn -k "C:\mykeys\mykeys.key"
Step 3 : Sign the component with the key
A key is generated, but it is not yet associated with the project's assembly. To establish the association, double-click the AssemblyInfo.vb file in the Visual Studio .NET or Visual Studio 2005 Solution Explorer. Add the following to the list of assembly attributes that are included in this file by default when a project is created in Visual Studio .NET or in Visual Studio 2005:
Now recompile the project and the assembly will be signed.
Below is a listing of the complete code of assembly AssemblyInfo.vb
The next step is to install the assembly into the Global Assembly Cache.
Step 4 : Host the signed assembly in Global Assembly Cache
There are two ways to install an assembly into the global assembly cache:
Using Microsoft Windows Installer 2.0
This is the recommended and most common way to add assemblies to the global assembly cache. The installer provides reference counting of assemblies in the global assembly cache, plus other benefits.
Using the Global Assembly Cache tool (Gacutil.exe)
You can use Gacutil.exe to add strong-named assemblies to the global assembly cache and to view the contents of the global assembly cache.
Gacutil.exe is only for development purposes and should not be used to install production assemblies into the global assembly cache.
Syntax to install a strong-named assembly into the global assembly cache
You can install the .dll file by using the Gacutil Tool or by dragging the .dll file into the appropriate directory. If you use the Gacutil Tool, you can use the following command:
At the command prompt, type the following command:
gacutil I <assembly name>
In this command, assembly name is the name of the assembly to install in the global assembly cache.
The following example installs an assembly with the file name myGAC.dll into the global assembly cache.
gacutil -I "C:\[PathToBinDirectoryInVSProject]\myGAC.dll"
After hosting the assembly just go to WINNT\Assembly folder and you will find your assembly listed there.
Step 5 : Test the assembly
Once you've installed the assembly into the GAC, you can use it from other programs by creating a reference.
Now, we will create a sample client application which uses our shared assembly.
In our sample clientGACTest project, expand the project and then right-click on the References item. Select "Add Reference" from the popup menu, and the Add Reference dialog box is displayed.
To refer an assembly in the GAC, just click on the Browse button and browse to the directory (myGAC/bin/debug) that contains the assembly (myGAC.dll). Locate the assembly, select it, and click OK.
Click on the myGAC reference in Solution Explorer and then examine the Properties pane. You'll see that the Copy Local property is set to False, Strong Name is true, and that the assembly version is 1.0.2369.37199
Just create a sample code as listed below :
You can now compile and run the clientGAC program.
Public Class SampleTest
Shared Sub main()
Dim x As New HelloClass()
Dim s As String = x.GetData()
Now, copy the resulting EXE in any other folder and run it. It will display "Hello World" indicating that it is using our shared assembly.
This article describes how to generate a strong name for an assembly and to install a .dll file in the Global Assembly Cache. The Global Assembly Cache (GAC) enables you to share assemblies across numerous applications. The GAC is automatically installed with the .NET runtime. Components are stored in C:\WINNT\Assembly.
By Bharati Karedla