Creating a Shared Assembly

K Pravin Kumar Reddy replied to chitanya chitanya 20-Feb-07 07:40 AM

Creating a Shared Assembly

By using a shared assembly, you can freely move the executable file around without having to worry about copying a DLL or .NetModule file with it. The assembly resides in the Global Assembly Cache, and the Runtime code knows where to find it. Creating a shared assembly requires several steps: 1. 2. 3. 4. Compile the source file to produce a .NET Module file with an extension of .NetModule. Create a public and private encryption key pair using the Strong Name utility, Sn.exe. Create the assembly, adding the encryption key pair to it. Add the shared assembly to the global assembly cache using the Global Assembly Cache tool GacUtil.exe.

How it is shared into GAC
Let us see the above steps how these steps are implemented with a small sample The SampleAssembly example contains one method

namespace SampleAssembly { private void function() { Response.Write("Hello,This is the sample Application illustrating Shared Assembly Creation"); } }

The first step is just compile your source code so that you will get .Net Module or DLL The Second step is to generate a key file, which contains unique Assembly name How can one create a unique Assembly name? Let us see Microsoft now uses a public-private key pair to uniquely identify an assembly. These keys are generated by using a utility called SN.exe (SN stands for shared name). Issue the following command at the command prompt

sn -k mykeyfile.snk

Now your key file is ready and is stored in the same folder After generating the strong name key pair file, it is required to associate this file with our assembly, for doing that you have to add the following lines in the code of your assembly.

Using System.Reflection; namespace SampleAssembly

Note here that the information regarding the file containing the strong name key pair is placed in the code file before the namespace declaration. Also you are required to import the System.Reflection namespace in order for the statement to work, otherwise the compiler would be showing you an error stating that it does not recognize the statement.

csc SampleApplGAC.cs /t:library /a.keyfile:sample.key

After compiling the assembly with the statements, containing the strong name information being added to it, you now have to place the assembly into the GAC. You can either do it manually by simply copying and pasting the assembly into the GAC, which is located at; c:\winnt\assembly or you can also use a utility gacutil that is installed with the .NET framework. For adding an assembly you would write as follows on the command prompt. gacutil /i sample.dll Here the /i option is for installation. And for removing an assembly from the GAC, you can use the same utility as follows. gacutil /u sample.dll The /u option here is for uninstalling or removing an assembly from the cache.

reference

Sign up to vote on this title
UsefulNot useful