Tuesday, March 01, 2011

Visual Studio External GAC Tool


Binary Contemplation
Binary Easter Egg
If you develop in a DLL Hell like I do, it can become tedious to GAC each time you build your class library and want to run a test against your changes. Our product shares an array of files across hundreds of Visual Studio solution projects, in order for dependencies to pick up changes made on the various objects, you have to deploy the assembly to the global assembly cache or GAC. To make matters worse, our team development sandbox is a non-isolated model and the builds deploy to a network shared directory. Therefore each time a change is made, rather, each time I build the class library, I have to point my Visual Studio Command Prompt to the Network Directory where our debug binaries are built, and run the Gacutility:

gacutil [options] [assemblyName | assemblyPath | assemblyListFile]

Maybe it's just me, but I found myself getting a little lazy after doing this several times a day. I looked at all the diverse set of tools in Visual Studio 2008 to look for a command to add in my toolbar, a button, a short-cut, anything. To my dismay, I came out empty handed. Online forums pointed to the MSDN Library and advises two methods to deploy your assemblies into cache:
·         Use an installer designed to work with the global assembly cache. This is the preferred option for installing assemblies into the global assembly cache.
·         Use a developer tool called the Global Assembly Cache tool (Gacutil.exe), provided by the Windows Software Development Kit (SDK).

So I figured I could create my own menu command to add my binaries to the Global Assembly Cache from Visual Studio, with my own external tool. It’s very easy to do by following these few simple steps: create a command file, add the cmd file to an external tool and pass target name and extension as arguments.

Cmd File 
In your favorite text editor, create a batch file. (For the record, I like writing the words “In your favorite text editor”, it gives me a chance to mention my editor of choice, Notepad ++). The trick is that you know the Visual Studio Command Prompt is really just a glorified DOS prompt. First step on the batch is to redirect to your Visual Studio directory, then execute the vcvarsall.bat file, which starts the Visual Studio tools SDK on the DOS prompt, hence, converting the DOS prompt to a Visual Studio Command Prompt. Then change directory to your development environment where your class library will build the DLL. Lastly, add the gacutility command and options with an argument parameter (%1). 

cd c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\
vcvarsall.bat
cd c:\development
gacutil /if %1

UPDATE

I am amending that by running the vcvarsall.bat, unless your DDL built to the Visual Studio 9.0 directory, it may not change directory to push the gacutil command on your class library. Instead, you can run the utility without having to execute the Visual Studio Tool environment. 

cd c:\development
gacutil /if %1




External Tools
In Visual Studio, open the Tools menu and click on "External Tools". Add a new tool, and name it, GacCmd on the title. At the command text box, include the path to the above .cmd file. In the arguments text box, choose Target Name and Target Extension. You will see this $(TargetName)$(TargetExt). Choose to Use Output Window. When complete, this will add GacCmd under the Tools menu. This will execute the .cmd file and pass the Target Name and Extension as arguments to the file. You will be able to see the execution in the Output window and see the message of file name "successfully added to assembly cache."

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Our approach is to have the gacutil call done in a post build event which eliminates new developers having to remember to push a button.
    http://billfellows.blogspot.com/2010/03/post-build-event.html
    and
    http://billfellows.blogspot.com/2009/09/visual-studio-build-macros.html

    ReplyDelete
  3. I knew that if I posted a tip, I'd find other approaches after the fact. Thanks for the advise Bill.

    ReplyDelete