Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, August 30, 2006

Shared Assemblies in ASP.NET

A shared assembly is always unique. To be uniquely identified, a shared assembly has a strong name. The combination of a file name, a public key, a version number and culture (locale details) gives an assembly a strong name, which is guaranteed to be unique. An assembly has a four part version number e.g. The parts are <Major> . <Minor> . <Build> . <Revision>.


Building the keys

To build public and private keys we have to use the sn (short for strong name) utility. This utility generates a public/private key pair. The public key created using this utility will be used to define a strong name. To create the key pair we must type the following at command prompt:


C:\CSharp\fontassembly>sn –k mykey.snk


On executing the utility a file called mykey.snk gets created. This file contains the public and private keys.The attribute must be either set to an absolute path to the key file or if the file is copied to the local directory of the project, only the file name must be mentioned. We need to add the following to the ‘AssemblyInfo.cs’ file.

[assembly: AssemblyKeyFile(“mykey.snk”)]

We should also change the version number of our assembly as shown below.[assembly: AssemblyVersion(“”)]

On rebuilding the program, the assembly would now use the public key from the mykey.snk file along with the name of the assembly, the version number and the culture to build a strong name. Culture is a combination of a two-letter word indicating the language and a two-letter word indicating the country code. The strong name never contains the private key. This ensures that the private key is not written in the manifest and hence is not made available to everybody. This strong name is written in the manifest of the assembly.


To create a small Class Library project by using Visual Studio .NET, to generate a strong name, and to install the project's .dll file in the Global Assembly Cache, follow these steps:

1. Open Visual Studio .NET.


2. Create a new Class Library project named GAC in Visual Basic .NET.


3. A strong name is needed. To generate this cryptographic key pair, use the SN Tool, which is located in the \bin subdirectory, where the .NET Framework Solution Developer Kit (SDK) is installed. The SN Tool is very easy to use. The command-line statement takes the following form:

sn -k "C:\[DirectoryToPlaceKey]\[KeyName].snk" 


4. For convenience, create a directory named GACDemo 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. Before you type the following SN command, you may want to copy this similar path on your computer to the .NET bin directory, type cd from the command prompt, right-click to paste the path, and then press ENTER to quickly change the directory where the SN Tool is located.


Type the following:

sn -k "C:\GACDemo\GACkey.snk"


5. A key is generated, but it is not yet associated with the project's assembly. To create this association, double-click the AssemblyInfo.vb file in the Visual Studio .NET 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:

<Assembly: AssemblyKeyFile("C:\GACDemo\GACKey.snk")>

Compile the project by clicking CTRL+SHIFT+B. No further code is necessary at this point in order to install a .dll file in the GAC. 


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

gacutil -I "C:\[PathToBinDirectoryInVSProject]\gac.dll"

If you want to drag the file, use Microsoft Windows Explorer. Open two instances of Windows Explorer. In one, navigate to the location of the .dll file output for your console project. In the other, navigate to c:\[SystemRoot]\Assembly.


Drag your .dll file into the folder.


You can also choose to delay sign your assembly. This means that the space for the signature is reserved in the PE file, but the signing itself is not done until later. This approach is typically used if you do not have access to the private key you need to generate the strong name. For more details, see How do I... Use Delay Signing.


gacutil -I "D:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\GAC\bin\gac.dll"

each time after complining theminor version will be changed

Post a Comment