CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

.Net – Assemblies – How to Reference Assemblies?

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.

Let’s 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 the Hello object. Do you remember, in our previous article, we have created a Hello assembly where we implemented the Hello class? Here we are using the Hello class from Hello assembly. But where are we referencing Hello assembly into the testapp.cs? We are referencing it at the time of compiling the testapp.cs. Here is the command:

csc testapp.cs /r:hello.exe

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

Remember that we should keep hello.exe and testapp.exe files in the same folder. Otherwise, the testapp.exe application will through the error; because it is referencing hello.exe which does 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.

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 the command prompt, it should install hello.exe into Global Assembly Cache. Let’s run and check the results.

When we ran at the command prompt; it displays the 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

Why it is showing an 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.

Giving a strong name to the Assembly

Generate the strong name using the Strong Name Tool (sn.exe). Below is the command:

sn -k hello.key

This tool with the 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 a hello.exe file with public key information in it. Let’s 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 the 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 did it fails 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, again we need to compile and re-generate testapp.exe with the 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 are modified? Not necessarily. We will discuss this in our upcoming articles.

.Net – Assemblies – How to Reference Assemblies?

One thought on “.Net – Assemblies – How to Reference Assemblies?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top