Creating and Installing DLL to GAC

Imagine scenarios below:

I want to install my libary (.dll) into Global Assembly Cache (GAC). In order to do that, my .dll must be given a strong name key (.snk)

In this article, I’m going to share my understanding about .dll with .snk and after that installing dll to GAC.

  1. Open Visual Studio and select File – New – Project to create new project.
  2. In New Project form, select Templates – Visual C# – Class Library. In Name: Utilities, Solution name: MyProduct, and the project is located in C:\Projects\.
    New Project
  3. Click Ok. MyProduct solution will be created and Class1.cs appears in VS.
    Class1
  4. Right click Class1.cs in the Solution Explorer and rename it to StringExtension. Click Yes if confirmation box appears.
  5. Now we have StringExtension class.
  6. Add constant MyString to the code.
    private const string MyString = "Im thrown from Utilities";
  7. Create method ReturnString that will return MyString.
    public string ReturnString()
    {
    	return MyString;
    }
    
  8. And this is the full code.
    namespace Utilities
    {
    	/// <summary>
    	/// Extension class for string
    	/// </summary>
    	public class StringExtension
    	{
    		/// <summary>
    		/// My string
    		/// </summary>
    		private const string MyString = "Im thrown from Utilities";
    
    		/// <summary>
    		/// Returns the string.
    		/// </summary>
    		/// <returns></returns>
    		public string ReturnString()
    		{
    			return MyString;
    		}
    	}
    }
    
  9. Build the solution ( Ctrl + Shift + B ).
  10. Now, try to take a look on the .dll
  11. Right click Utilities project, select Open Folder in File Explorer.
    Open Folder in File Explorer
  12. Open bin directory.
  13. Open Debug directory. That’s it! Our Utilities.dll stay there.
    Utilities.dll

So far, we already created the .dll. Try to install this one into GAC.

  1. From Start menu, open All Programs – Microsoft Visual Studio – Visual Studio Tools – Developer Command Prompt for VS.
    Start menu
    Sometimes, we need to run it as administrator, so we can install our .dll into GAC directory.
  2. Developer Command Prompt for VS is opened.
    Developer command prompt
  3. Type in: cd C:\Projects\MyProduct\Utilities\bin\Debug
  4. Install Utilities.dll with command: gacutil /i Utilities.dll
  5. Ulala! Failure adding assembly to the cache: Attempt to install an assembly without a strong name.

Utilities.dll that we have created before doesn’t has strong name key. So we need to add this .snk things to our .dll.

  1. Open MyProduct solution again.
  2. Right click Utilities project and select Properties. Or you can just type shortcut Alt + Enter.
  3. Move to Signing tab. We can see Sign the assembly there.
    Sign the assembly
  4. Tick on Sign the assembly.
  5. In Choose a strong name key file, select <New…>
  6. Give a Key file name: MyProduct.
    Create strong name key
  7. For now, just uncheck Protect my key file with a password.
  8. Signature Algorithm: sha256RSA.
  9. Click Ok and MyProduct.snk has been created.
    MyProduct snk
  10. Build the solution again ( Ctrl + Shift + B ).
  11. Open Developer Command Prompt for VS.
  12. Try to install the Utilities.dll once again: gacutil /i Utilities.dll
  13. DLL installation is succeed. Assembly successfully added to the cache.
    Assembly successfully added to the cache

Since the platform target of our project is Any CPU, our .dll will be located in
C:\Windows\Microsoft.NET\assembly\GAC_MSIL.
GAC_MSIL
Just open the path via Windows Explorer and you can see Utilities.dll stays there. Try to change the platform target – build – and install again. If the platform target is x86, the .dll will be installed in GAC_32 or x64 to install it in GAC_64 directory.

Last, to uninstall Utilities.dll from GAC, use syntax: gacutil /u Utilities.
Uninstall DLL from GAC

I think that’s all for Creating and Installing DLL to Global Assembly Cache. In next article, I’m going to share how to Create Installer using WiX technologies. So we can install or uninstall .dll from GAC without open Developer Command Prompt.

Leave a Comment

Capcay *