C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




how to create shared assembly


Posted Date: 06 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Kumar Velu       Member Level: Diamond
Rating:     Points: 10



following article describes how to create shared assembly:

Description:
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.")

About this article:

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.

Steps:

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:



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














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




Responses

Author: Mahesh Raj    07 Jun 2008Member Level: Gold   Points : 1
This is very good information,Continue posting such useful articles.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: The Padding Ball game in WPF
Previous Resource: Printing HTML Pages in Windows Applications using SHDocVw.dll
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

it help desk

Contact Us    Privacy Policy    Terms Of Use