Introduction
In order to share a .NET assembly with multiple applications installed on the same machine, it needs to be installed as 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 cls for Finance module to be used across 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 given amount, rate of interest and period. In order for other applications to be able to use the functionality in the above class, we need to install it in the GAC. To qualify 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 into 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 is having 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 shell extension viewer for an entry of 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, System.EventArgs e)
{
Finance.Finance MyInt=new Finance.Finance();
doubleSI = MyInt.ComputeInterest(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text), Convert.ToInt32(textBox3.Text));
textBox4.Text =SI.ToString();
}