Assemblies in .NET

How Can Assemblies Avoid DLL Hell?

Most assemblies are private. Hence, each client application refers assemblies from its own installation folder. So, even though there are multiple versions of the same assembly, they will not conflict with each other. Consider the following example:

When you create a private assembly, an assembly is installed in a subdirectory of the application, so even if two assemblies have the same name there is no problem because an application will always refer to its own assembly. Now, consider the case when you develop a shared assembly. In this case, it is important to know how assemblies are versioned. All assemblies have a version number in this form:

If you change the original assembly, the changed version will be considered compatible with the existing one if the major and minor versions of both the assemblies match. When the client application requests an assembly, the requested version number is matched against available versions and the version matching the major and minor version numbers and having the latest build and revision numbers are supplied.

How Do I Create Shared Assemblies?

The following steps are involved in creating shared assemblies:

  1. Create your DLL/EXE source code.
  2. Generate a unique assembly name using SN utility.
  3. Sign your DLL/EXE with the private key by modifying the Assembly Info file.
  4. Compile your DLL/EXE.
  5. Place the resultant DLL/EXE in a global assembly cache by using the AL utility.

How Do I Create a Unique Assembly Name?

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). The most common syntax of it is:

sn -k mykeyfile.key

where k represents that you want to generate a key and the file name following is the file in which the keys will be stored.

How Do I Sign My DLL/EXE?

Before placing the assembly into a shared cache, you need to sign it by using the keys you just generated. You mention the signing information in a special file called AssemblyInfo. Open the file from VS.NET solution explorer and change it to include the following line:


Now, recompile the project and the assembly will be signed for you.

Note: You also can supply the key file information during a command line compilation via the /a.keyfile switch.

How Do I Place the Assembly in a Shared Cache?

Microsoft has provided a utility called AL.exe to actually place your assembly in shared cache:

AL /i:my_dll.dll

Now, the utility will place your DLL at the proper location.

Hands On…

Now that you understand the basics of assemblies, you can apply your knowledge by developing a simple shared assembly. In this example, you will create a C#.NET component called SampleGAC (GAC stands for Global Assembly Cache). You also will create a key file named sample.key. You will sign your component with this key file and place it in the Global Assembly Cache.

Step 1: Creating your sample component

Here is the code for the component. It just includes one method that returns a string.

using System;
namespace BAJComponents
   public class Sample
      public string GetData()
         return "hello world";

Step 2: Generate a key file

To generate the key file, issue the following command at the command prompt.

sn -k sample.key

This will generate the key file in the same folder.

Step 3: Sign your component with the key

Now, you will sign the assembly with the key file you just created.

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

Step 4: Host the signed assembly in the Global Assembly Cache

You will use the AL utility to place the assembly in the Global Assembly Cache:

AL /i:sampleGAC.dll

After hosting, the assembly just goes to the WINNTAssembly folder and you will find your assembly listed there. Note how the assembly folder is treated differently than normal folders.

Step 5: Test that your assembly works

Now, create a sample client application that uses your shared assembly. Just create a sample code as listed below:

using System;
using BAJComponents;
public class SampleTest
   static void main()
      sample x= new sample();
      string s= x.getdata();

Compile the above code by using:

csc sampletest.cs /t:exe /r:<assembly_dll_path_here>

Now, copy the resulting EXE in any other folder and run it. It will display “Hello World”, indicating that it is using your shared assembly.

Sameer Sood
Computer Science and Engineering, Third Year
NIT, Durgapur.

More by Author

Must Read