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:

major.minor.build.revision

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:

[assembly:AssemblyKeyFile("file_path")]

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 WINNT\Assembly 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();
      console.writeline(s);
   }
}

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.



Comments

  • ~!@@#%$#^$%&&*^&*^&*&%*&^*&^*^&*&^

    Posted by ~!@@#%$#^$%&&*^&*^&*&%*&^*&^*^&*&^ on 01/19/2013 03:49am

    ~!@@#%$#^$%&&*^&*^&*&%*&^*&^*^&*&^

    Reply
  • Please test your tut before posting it....

    Posted by Navdeep on 11/14/2006 04:27am

    Sammer, This line - csc sampleGAC.cs /t:library /a.keyfile:sample.key is incorrect and wont work with vs.Net2005. Please post correct and tested code. This is upsetting because there are people out there that become so confused when they follow a tut and they have to debug it. Thanks...

    • RE: Please test your tut before posting it....

      Posted by sameer_sood on 11/20/2006 02:55pm

      hi
      i was not able to login into my account , hence the delay , the code is not for vs 2005, and works perfectly with .net framework 1.1,also about reviewing, before any article is published on codeguru it is thoroughly analyzed by its editors,so they have tested it too.
      regards
      sameer sood

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds