Install a shared Assembly to the GAC as post build event from Visual Studio

By | September 14, 2011

The Global Assembly Cache (GAC) is a Cache for globally available Assemblies. Most of the shared assemblies are installed inside GAC to shared across different applications. The Global Assembly Cache tool (Gacutil.exe) allow us to manipulate the contents of the global assembly cache along with install and un-install Assembly. During the development, we generally  take help of gacutil from command prompt to install or uninstall assemblies into or from GAC. This is a repetitive process, every time you made some changes in your library you might need to deploy it into the GAC. Visual Studio has a nice feature called Pre-Build and Post-Build events. Using these events we execute some specific  commands before or after the build. In this post I am going to show you how we can configure the post build event to deploy an Assembly to GAC.

  Installing Assembly to the  GAC requires admin privileges. Visual Studio must be started with elevated rights.  From the Project Properties Window  Page of the class library ( which you want to put into GAC) , Navigate to “Build Events” Tab.  Build Events tab having two command line option for both Pre-Build and Post-Build event.  We will be configuring the Post Build Event to install the dll into GAC because we want to put the dll into GAC after the successful build.

Below is the  typical syntax for installing Assembly into GAC  from command prompt which we used generally

gacutil [options] [assemblyName | assemblyPath | assemblyListFile]

Visual Studio Build Events also use the same syntax. But, the interesting part is the Location of GAC.  Yes, the GAC was split into two. We all knows, .NET 4.0 introduced CLR 4.0. NET Framework 2.0 and .NET Framework 3.5 targeted to  CLR 2.0  and  .NET Framework 4.0 is targeted to CLR 4.0. The GAC is now split into private GAC’s for each runtime that targeted differently to CLR 2.0 and CLR 4.0 . I will recommend you read the article  “Understanding the CLR Binding” for more details.

Let’s assume, the Assembly is targeted to .NET 4.0 . To Add this Assembly, we have to specify the below Pre-Build Command.

"%ProgramFiles%\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\gacutil.exe" /i "$(TargetPath)"

image

$(TargetPath) is a pre defined macros that returns the dll full path.

image

Ok, that’s all. Save the file and build  your class library.  You are expecting the assembly to be installed into GAC.  Well, this will . But before that you need to sign the assembly for a strong name.  Once you have assembly with strong name, it will install the assembly into GAC. 

Read the complete post from here