Every so often you come across a project where you need to install a strongly typed assembly into the gac in order to run your application. What happens when you need to debug the application and you have modified the assembly? You have to move the new version into the gac.
What if there was a way to move the assembly into the gac automatically when the project was successfully built?
In fact there is. Right click on your project and select properties from the menu. In the window that open there are a few tabs; the one we are interested in is "Build Events". Now there are a few ways you can write your build actions which will work. Personally, I like to write the commands to a bat file and run the bat file. If something goes wrong, I can look at the code that was run, modify it and re run it.
Keeping with the example of copying an assembly into the gac the are a couple of things I should mention
- gacutil.exe is installed with the .net framework v 1.x not 2.x or 3.x
- the gacutil.exe from 1.x is not compatible with the .net 2 or 3 frameworks.
- the gacutil.exe from 3.x does not exist. It uses the 2.x gacutil.exe
- the gacutil for 2.x is in the 2.x sdk and you must install the sdk to have the exe.
- The gacutil.exe is not licensed for redistribution; so do not included in your final project. Besides, your products setup project should add the assembly to the gac.
Having said that (#5 is important) I added the gacutil to my Class Library project and marked it to be copied to the output directory along with the gacutil.exe.config file (if they are newer) to make my build event code a little shorter. In this case the assembly is being install on my local machine and the project is for a visual studio template wizard so I am not really worried about the licensing issue.
There are 3 fields a pre-build, post build and run the post build event.
I will start at the bottom of the screen and work my way up.
- Run the post-build event: I set this to "When the build updates the project output" or "On successful build". Really the only time it needs to run is when the output has changed.
- Post-build event: is set to
$(TargetDir)installGac.bat - Pre-build event"
echo $(TargetDir)Gacutil.exe /i $(TargetDir)$(TargetFileName) /f > $(TargetDir)installGac.bat
Signing the assembly
Now we need to sign the assembly so we can add it into the gac. Move down to the signing tab and check the "sign the assembly" checkbox. In the "Choose a strong name key file" dropdown list you can select a snk file you already have of select <new...>, use new for now.
Right away you get a box that wants a key file name and for you to create a password.
The password is important. If you set one be sure you have it someplace. If someone else tries to use the project they will need the password as well. For now you can clear the "protect my key file with a password and put in a name (I am using RogerRoger as the name for reasons that I do not know. Anyplace I refer to RogerRoger, just substitute the name you used.). Generally, you should use a helpful/descriptive name. Hit ok and clear the "delay sign only" checkbox. Hit ok again.
You will see the file RogerRoger.snk has been added to your project...
If you build the project and and go look at your gac ( Windows start menu -> settings -> Control Panel -> Administrative Tools -> Microsoft .NET Framework 2.0 Configuration. select the "Manage the Assembly Cache" from the Tasks on the right and click on "View List of Assemblies in the Assembly Cache") you should see your newly added assembly in the list.
When would you need to add an assembly to the gac? I made a custom wizard for Visual Studio and I needed to add the assembly to the gac.
In your bin directory you should see something like this...
Build actions can also be used to copy files from on location to another, run something via the command line or launch some other process like a call to sandcastle.