Referencing GAC Assemblies With Visual Studio 2005
Posted by Ken on August 15, 2009
In Visual Studio 2005, adding an assembly reference is straight-forward and trivial, that is, unless the assembly you are trying to reference only exists in the GAC. There are a number of tabs in the Add Reference dialog, but they browse assemblies that are all located in standard directories.
Global Assembly Cache
The GAC is special because it allows a pc to store multiple versions of the same .NET assembly. This is made possible by strong naming. More importantly, it provides a centralized location for .NET assemblies that need to be shared across multiple applications.
When viewing the GAC via the Windows shell, you go here: C:\Windows\Assembly. However, the Explorer-like UI actually represents an aggregate of files and folders. Windows is just trying to make it look nice and user friendly for us.
Tricking Visual Studio
To trick Visual Studio into referencing a GAC assembly, you can do the following:
1.) Reference a copy of the assembly that is located outside of the GAC via the Add Reference dialog. You can throw a copy on your Desktop and then reference it from there. It doesn’t matter what the file’s location is.
2.) Install the assembly into the GAC. There are a number of ways to do this. The easiest way is to use the “Microsoft .NET Framework 2.0 Configuration” app. You can find it by going to Control Panel –> Administrative Tools. Once in the app, click “Manage the Assembly Cache” and then “Add an Assembly to the Assembly Cache.”
3.) Delete the assembly file that you referenced in step 1. Go into Visual Studio and refresh the project. The path of the reference should have automatically changed over to its GAC location (C:\Windows\assembly\…).
4.) You may want to set the reference’s “Copy Local” property to False so that the build does not produce a copy of the GAC assembly in the bin folder (to avoid confusion). In doing so, you can be confident that your app is in fact hitting the GAC!
Mark said
If all of your programs use the GAC’s version of an assembly are they locked in to a specific version of said assembly? Is there a way to configure them to use the latest version?
Ken said
If you have Foo.dll 1.0 and Foo.dll 2.0 installed in your GAC and they are backwards compatible, you can install a publisher policy file into the GAC which will redirect calls from 1.0 to 2.0: http://msdn.microsoft.com/en-us/library/7wd6ex19(VS.80).aspx
However, if they are not backwards compatible, you need to manually change the project references to hit the newest version. Tip: When using the trick demonstrated above, VS will pick the newest version of the assembly for you.
Hope that helps!
Lotta said
I bow down humbly in the presence of such grnetaess.
ltufzulhcj said
MiOguF kvumcjmulhsf
Lizabeth said
Your story was really inrfomtiave, thanks!
mtckmjfezqx said
8W5Caf acvfwmlgioci
Ken said
Creating a publisher policy: http://msdn.microsoft.com/en-us/library/dz32563a(VS.80).aspx