.NET: Install a DLL to GAC (Global Assembly Cache) Windows Server 2012

Prior to Windows Server 2012 I had been use to installing DLL files in the Windows Global Assembly Cache (GAC) by either opening the Windows/Assembly folder in Explorer and simply dragging and dropping the file, or by using GacUtil.exe

With Windows Server 2012 unfortunately it’s not quite so easy. Being able to simply open the GAC in Explorer and drag/drop is gone. Also GacUtil.exe is not present on the server by default.

To use Gacutil on a non-development machine you will have to copy the executable and config file from your dev machine to the production machine. It looks like there are a few different versions of Gacutil. The one that worked for me, I found here:

  1. Copy the files gacutil.exe files fromC:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe
    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe.configto the appropriate .net folder
    C:\Windows\Microsoft.NET\Framework\v2.0.50727
    C:\Windows\Microsoft.NET\Framework\v4.0.30319
  2. Copy the “VS2013 x86 Native Tools Command Prompt” shortcut from windows 7 pc (where visual studio command has been installed correctly) to windows server 2012 into
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts folder.
  3. Run this “VS2013 x86 Native Tools Command Prompt” tools as administrator.
  4. Then use these commands to uninstall and reinstall respectively

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\gacutil.exe" /i "D:\Projects\myDLL.dll"

“C:\Users\BHJeremy\Desktop\Installing to the Gac in .net 4.0\gacutil.exe” /u “myDLL”

 

If you have necessary .net framework installed. Ex ; .Net 4.0 or .Net 3.5, then you can just copy Gacutil.exe from any of the machine and to the new machine.

1) Open CMD as adminstrator in new server.
2) Traverse to the folder where you copied the Gacutil.exe. For eg – C:\program files.(in my case).
3) Type the below in the cmd prompt and install.

C:\Program Files\gacutil.exe /I dllfilename.dll

 

In .net 4.0 Microsoft removed the ability to add DLLs to the Assembly simply by dragging and dropping.

Instead you need to use gacutil.exe, or create an installer to do it. Microsoft actually doesn’t recommend using gacutil, but I went that route anyway.

  1. Copy the “VS2013 x86 Native Tools Command Prompt” shortcut from windows 7 pc (where visual studio command has been installed correctly) to windows server 2012 into
    C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts folder.
  2. Run this as administrator.
  3. To use gacutil on a development machine go to: Start -> programs -> Microsoft Visual studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010). Then use these commands to uninstall and Reinstall respectively. Note I did NOT include .dll in the uninstall command.

gacutil /u myDLL

gacutil /i "C:\Program Files\Custom\myDLL.dll"

 

PowerShell to the rescue. Here’s how to register a DLL called “MyDLL.dll” to the GAC (and also how to remove it.

For this example assume we have the “MyDLL.dll” file stored at c:\temp\MyDLL.dll

To add a DLL to the GAC

1. Run the SharePoint PowerShell console as Administrator

2. Enter the following PowerShell

1
2
3
4
5
Set-location "c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("c:\temp\MyDLL.dll")
iisreset

To remove a DLL from the GAC

1. Run the SharePoint PowerShell console as Administrator

2. Enter the following PowerShell

1
2
3
4
5
Set-location"c:\temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish= New-ObjectSystem.EnterpriseServices.Internal.Publish
$publish.GacRemove("c:\temp\MyDLL.dll")
iisreset

How to install a DLL to the GAC on Windows Server 2012 using only PowerShell (without having to install SDK or Visual Studio) here’s how you can find the PublicKeyToken for a DLL using only PowerShell.

([system.reflection.assembly]::loadfile("c:\temp\MyDLL.dll")).FullName

The output of this PowerShell statement will provide the Version, Culture and PublicKeyToken as shown below.
MyDLL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=630d986a50055aa6