Low Down on Installing a .NET Assembly into the GAC

Introduction

To share a .NET assembly with multiple applications installed on the same machine, it needs to be installed as a Shared Assembly in the Global Assembly Cache (GAC). This article will walk you through the process of giving your assembly a strong name and installing it into the GAC.

Example

using System;

namespace Finance
{
    /// <summary>
    /// Example class for the Finance module to be used across a host of applications and to be installed in GAC
    /// </summary>
    public class Finance
    {
        public double ComputeInterest(int rateInterest, int principalAmount, int periodDeposit)
        {
            double simpleInterest = (rateInterest * principalAmount * periodDeposit) / 100;
            return simpleInterest;
        }
    }
}

The above class computes simple interest for a given amount, rate of interest and period. For other applications to be able to use the functionality in the above class, we need to install it in the GAC. To qualify for Assembly in GAC, it should be assigned a Strong Name.

What is a Strong Name?

A strong name is made up of the full class name including the namespace, the version number, culture information (which can be null if culture neutral), plus a public key and a digital signature.

The .NET Framework provides a utility to create a key pair (sn.exe). Run the following at a VS.NET command prompt.

Setting environment for using Microsoft Visual Studio .NET tools. (If you also have Visual C++ 6.0 installed and wish to use its tools from the command line, run vcvars32.bat for Visual C++ 6.0.).

C:\>sn -k Financials.snk

Microsoft (R) .NET Framework Strong Name Utility Version 1.0.3705.0.

Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Key pair written to Financials.snk

C:\>

The -k option generates the random key pair and saves the key information in the Financials.snk file. We use this file as input when we build our Shared Assemblies.

Add the generated key to the current solution. Modify AssemblyKeyFile & version in AssemblyInfo.cs as follows.

[assembly: AssemblyTitle("Compute Interest")]
[assembly: AssemblyDescription("Calculates Simple Interest")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("My Inc.,")]
[assembly: AssemblyProduct("Interest Server Application")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("en-US")]
[assembly: AssemblyKeyFile("Financials.snk")]
[assembly: AssemblyVersion("1.0.0.0")]

Thus we have an assembly that has version, culture, and public key pair.

Then build the project.

Installing Assembly into the GAC

From a command prompt, in the same directory where Finance.dll resides, run gacutil.exe as below.

gacutil /i Finance.dll

Or

Open Windows Explorer,

Drag Finance.dll into C:\WinNT\Assembly folder

The assembly can now be used from other assemblies on the server, regardless of their physical location. Check the GAC through the shell extension viewer for an entry of the Finance component.

Create test C# Windows application project, reference the above assembly. The referenced DLL will have its Copy Local property as False, try running the above method from the client.

private void button1_Click(object sender, EventArgs e)
{
    Finance.Finance MyInt = new Finance.Finance();
    double simpleInterest = MyInt.ComputeInterest(
        Convert.ToInt32(textBox1.Text),
        Convert.ToInt32(textBox2.Text),
        Convert.ToInt32(textBox3.Text)
    );
    textBox4.Text = simpleInterest.ToString();
}


Similar Articles