Globally Accessible DLLs

By default, C# applications require any user created DLLs to be present in the same directory as the application. As DLLs are typically shared by multiple applications, it may be necessary for the DLL to be placed in a globally accessible location. To do this in C# you need to add the DLL file to the Global Assembly Cache (GAC).

Strong-Named Assemblies

Before a DLL can be added to the GAC, it must be given a strong-same. This procedure aims to protect the user from corrupted DLLs. As DLLs are linked at runtime, it would be possible for someone to build a new version of the DLL but add malicious code. The user application would have no way of telling that this is not the correct DLL and would run the malicious code. GAC and strong-named assemblies protect against this, for more information see Strong-Named Assemblies on the Microsoft website.

Creating a Strong-Named Assembly

The C# DLLs in the Nirvana download have already been given strong-names so this section is not required to make the Nirvana DLLs globally accessible.

  1. Either open a .NET command prompt or open a standard command prompt and run vsvars32.bat which is located in "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools". Which will set up the required environment.
  2. Navigate to a directory where you want to store the keyfile and run the following command:
    C:\myarea\folder\> sn -k keyfile.snk
    
    This will create a keyfile which contains a pair of private and public keys which can be used to protect your DLLs.
  3. Now you need to edit the AssemblyInfo.cs file for the project used to create the DLL by adding the following code:
    [assembly:AssemblyKeyFile(@"C:\myarea\folder\keyfile.snk")]
    
  4. Now when you build the DLL as usual it will be given a strong-name but will not be globally accessible until added to GAC.

Adding Strong-Named Assembly to GAC

  1. Either open a .NET command prompt or open a standard command prompt and run vsvars32.bat which is located in "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools". Which will set up the required environment.
  2. In this prompt execute gacutil as shown below:
    C:\myarea\folder\> gacutil /i mylib.DLL
    

The DLL will now be globally accessible on the system. The C#.NET sample applications in the download use the "Nirvana Dotnet.DLL" library and "nSampleApp.DLL", both have been given strong-names so can be added to GAC using gacutil as described above.

NOTE: to remove an assembly from the cache execute "gacutil /u mylib", the file extention is not required.

Share this page with others:
Tell Your Tweets Facebook It! Add to Delicious Reddit! Digg It! Stumble Upon Add to Your Faves Mixx it
Follow Us:
Keep up with my-Channels on Twitter Become a fan on Facebook LinkedIn Profile Recent Highlights RSS Feed