.Net: Assemblies (Part – 2)

We have created one simple private assembly hello.exe in our previous article. In this article, we will look at how to reference assemblies into our programs and how to call methods in assemblies.

Lets create a simple program testapp.cs. Below is the code:

// testapp.cs
//
using System;

namespace CodeSteps
{
    public class TestApp
    {
        public static void Main()
        {
            CodeSteps.Hello hello = new CodeSteps.Hello();

            hello.SayHello();
        }
    };
};

In the code, we created Hello object. Do you remember, in our previous article, we have created Hello assembly where we implemented Hello class? Here we are using the Hello class from Hello assembly. But where are we referencing Hello assembly into testapp.cs? We are referencing it at the time of compiling testapp.cs. Here is the command:

csc testapp.cs /r:hello.exe

Here we referenced hello.exe assembly through “/r” switch. Once it is compiled, it will generate testapp.exe. Run testapp.exe and observe that it will display “Hello, World!” message on command prompt console.

Remember that we should keep hello.exe and testapp.exe files in the same folder. Otherwise testapp.exe application will through the error; because it is referencing hello.exe which is not exist in the same folder where testapp.exe exists.

If multiple applications are using hello.exe, do we need to copy hello.exe in all application folders? The answer is yes and no. If you install hello.exe into Global Assembly Cache (GAC), no need to copy hello.exe in all application folders.

Then how to install the assembly hello.exe into GAC (Global Assembly Cache)?

Global Assembly Cache is the Cache where Windows maintains all the shared assemblies.

Now install our assembly hello.exe into GAC using gacutil tool. Below is the command:

gacutil /i hello.exe

Once you run the above command at command prompt, it should install hello.exe into Global Assembly Cache. Lets run and check the results.

When we ran at command prompt; it displays following message.

c:\Visual Studio Projects 2012>gacutil /i hello.exe
Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.17929
Copyright (c) Microsoft Corporation. All rights reserved.

Failure adding assembly to the cache: Attempt to install an assembly without a strong name

c:\Visual Studio Projects 2012>

Why it is showing error message? The reason is, before installing an assembly to GAC; it will check whether the strong name is provided to the assembly or not. If it is not, the test failed, hence the failure message.

Then how to give strong name to the assembly? Generate the strong name using Strong Name Tool (sn.exe). Below is the command:

sn -k hello.key

This tool with switch “-k” will generate a key pair and store it in hello.key file.

Now we have to add this key pair information into hello.exe. So, re-generate hello.exe with this key file.

csc hello.cs /keyfile:hello.key

It will generate hello.exe file with public key information in it. Lets open hello.exe using ildasm tool.

ildasm hello.exe

Observer that .publickey section added into assembly’s manifest.

Microsoft .Net - Assembly Manifest with .publickey section

Microsoft .Net – Assembly Manifest with .publickey section

Let’s try again installing hello.exe into GAC. Now it will install successfully.

Now again run testapp.exe from command prompt. Observe the following result.

c:\Visual Studio Projects 2012>testapp.exe

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'hello, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
   at CodeSteps.TestApp.Main()

c:\Visual Studio Projects 2012>

Why it failed this time? Because we have modified hello.exe by adding key pairs. testapp.exe referencing old hello.exe, which doesn’t exist; hence the Unhandled Exception.

To resolve this, agian we need to compile and re-generate testapp.exe with latest hello.exe assembly.

csc testapp.cs /r:hello.exe

Now testapp.exe will run successfully.

We can now delete hello.exe from the folder where testapp.exe exists. When we run testapp.exe, it will run successfully. This time testapp.exe will pick the assembly from GAC where we already installed our hello.exe assembly.

Do we need to re-generate the application whenever the reference assemblies modified? Not necessarily. We will discuss about this in our up-coming articles.

1 comment for “.Net: Assemblies (Part – 2)

Leave a Reply