What is a Shared Assembly?
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 C# 2010 class Library 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
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 c# 2010.
Here is the code for the component. It just includes one method which returns a string.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myGac
{
public class Class1
{
public string GetName()
{
return "Syed Nisar Bukahri";
}
}
}
Step 2 : Generating Cryptographic Key Pair using the tool SN.Exe
Now, we will use the SN tool to generate the cryptographic key pair.
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 visual studio command prompt.
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, open the AssemblyInfo.cs file in Visual Studio 2010 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\\mk.key")]
Note:Add this line just below the following line
[assembly: AssemblyTitle("ClassLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClassLibrary1")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
...Add this line here.......
Now recompile/build the project and the assembly will be signed.
The next step is to install the assembly into the Global Assembly Cache.
Step 4 : Host the signed assembly in Global Assembly Cache
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.
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.
Or if you are running windows 7 go to this location to locate the Assembly
C:\Windows\Microsoft.NET\assembly\GAC_MSIL
and here you will the folder in which is your signed assmbly
like myGac folder
In this will be another folder with name like
v4.0_1.0.0.0__e20b8219cca7baba
and in this folder will be your assemble stored with name as myGac.dll
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 (C:\Windows\Microsoft.NET\assembly\GAC_MSIL\v4.0_1.0.0.0__e20b8219cca7baba) 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,Aliases is global and that the assembly version is 1.0.0.0
Just create a sample code as listed below :
You can now compile and run the clientGAC program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myGac;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Class1 c = new Class1();
string str = c.GetName();
Console.WriteLine(str);
Console.Read();
}
}
}
And it will display the name "Syed Nisar Bukhari"
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:\Windows\Microsoft.NET\assembly\GAC_MSIL\ or C:\WINNT\Assembly.
Now time to practice!!!!
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 C# 2010 class Library 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
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 c# 2010.
Here is the code for the component. It just includes one method which returns a string.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myGac
{
public class Class1
{
public string GetName()
{
return "Syed Nisar Bukahri";
}
}
}
Step 2 : Generating Cryptographic Key Pair using the tool SN.Exe
Now, we will use the SN tool to generate the cryptographic key pair.
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 visual studio command prompt.
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, open the AssemblyInfo.cs file in Visual Studio 2010 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\\mk.key")]
Note:Add this line just below the following line
[assembly: AssemblyTitle("ClassLibrary1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ClassLibrary1")]
[assembly: AssemblyCopyright("Copyright © 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
...Add this line here.......
Now recompile/build the project and the assembly will be signed.
The next step is to install the assembly into the Global Assembly Cache.
Step 4 : Host the signed assembly in Global Assembly Cache
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.
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.
Or if you are running windows 7 go to this location to locate the Assembly
C:\Windows\Microsoft.NET\assembly\GAC_MSIL
and here you will the folder in which is your signed assmbly
like myGac folder
In this will be another folder with name like
v4.0_1.0.0.0__e20b8219cca7baba
and in this folder will be your assemble stored with name as myGac.dll
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 (C:\Windows\Microsoft.NET\assembly\GAC_MSIL\v4.0_1.0.0.0__e20b8219cca7baba) 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,Aliases is global and that the assembly version is 1.0.0.0
Just create a sample code as listed below :
You can now compile and run the clientGAC program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myGac;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Class1 c = new Class1();
string str = c.GetName();
Console.WriteLine(str);
Console.Read();
}
}
}
And it will display the name "Syed Nisar Bukhari"
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:\Windows\Microsoft.NET\assembly\GAC_MSIL\ or C:\WINNT\Assembly.
Now time to practice!!!!
No comments:
Post a Comment