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.
imports system 

namespace BuildGAC
	public class HelloClass
		public function GetData() as string 
			return ´hello, how are you doing!´ 
		end function 
	end class 
end namespace 

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.

NOTE:

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:

<Assembly: AssemblyKeyFile("C:\mykeys\mykeys.key")>

Now recompile the project and the assembly will be signed.

Below is a listing of the complete code of assembly AssemblyInfo.vb

Imports System.Reflection
Imports System.Runtime.InteropServices

<Assembly: AssemblyTitle("")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("")>
<Assembly: AssemblyCopyright("")>
<Assembly: AssemblyTrademark("")>
<Assembly: CLSCompliant(True)>
<Assembly: AssemblyKeyFile("C:\mykeys\mykeys.key")>


<Assembly: Guid("E3492A62-5389-4286-94A3-1331CC29EA6D")>
<Assembly: AssemblyVersion("1.0.*")>
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.

Note:

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.

Imports System 
Imports myGAC.BuildGAC

Public Class SampleTest
    Shared Sub main()
        Dim x As New HelloClass()
        Dim s As String = x.GetData()
        Console.WriteLine(s)
    End Sub
End Class

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.

Summary:

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.





cover image of azure cloud book

Develop Azure Applications

Azure Cloud is gaining popularity and is competing with Amazon Elastic Cloud and Google Cloud for a spot in the billion dollar Cloud computing space.

With this book learn how to use - Table (an Azure Table is different from a SQL Server Table), Queue, Blob storage services, use your local machine to create an Application and a Service, how Azure splits a Table to achieve Load Balancing, Scalability based on the Partition Key, AppFabric Service Bus, Access Control Service, role of Fabric Controller, features of Azure SQL Database and Content Delivery Network.

Many have mastered Cloud Programming and are enjoying the benefits. Why not you?

View Highlights » View Contents »
cover image of foundation database programming book

Create your Own Accounting Software

A foundation book to Master Accounting Software Development, VB.Net, C# and OOPs programming. Detailed explanation of the Accounting domain for programmers is included. SQL and ADO.Net is also covered in detail.

This book gives you a strong foundation in Application Development.

A thorough understanding of the contents will enable you to grasp the topics covered in the accompanying books quite easily. This is because, the accounting domain is dealt with great detail in this book.

The ideal book to get started with application development in the .Net world.

View Highlights » View Contents »

cover image of asp.net book

Create your Own WEB ERP Software using ASP.Net

This book opens you to the Web. Using ASP.Net, C# and SQL Server as the tools, you will learn to build a Multi Tier Web Application as recommended by Microsoft. You will learn implementing inline GridView control, Transaction Management for a layered Web Application, build Data Access component and a Business Layer component.

Opens you to a world of opportunities. You will be able to develop a Web ERP application including all the five modules and learn how to use the TreeView control and GridView control to implement transactions such as Purchase Invoice, Sales Order and Sales Invoice.

View Highlights » View Contents »
cover image of entity framework book

ASP.Net MVC 5 Application Development Using Entity Framework 6 and VS 2015

Entity frameworks is the latest buzz in .Net programming and is the data access technology for Microsoft OS based hand held devices and web applications. Learn to integrate EF into an ASP.Net Application and MVC application. Create, explore and query the Entity Data Model. Learn how to use ObjectDataSource control and GridView control in an ASP.Net Application.

Also, learn to build Three-Tier ASP.Net application, create a business-class, implement methods in the class to perform CRUD operations and bind a GridView to the ObjectDataSource control. Learn how to handle concurrency and managing Transactions.

View Contents »