Also checkout my blogs on: PMP | C# | SQL | Personal

  Thursday, February 09, 2006

Global Assembly Cache

If you are COM buff, you know that regsvr32 is a history in .NET. What does it mean? In COM, all the .dlls are registered in the registry with regsvr32 command. Once a dll is registered by this way, it can be accessed from any application running locally. Well, that was great achievement in COM days as one copy of the dll serves the need of multiple applications. Reusability, scalability were the buzz words.  Say, you have 5 applications hosted on your web server, all of them send out emails. So, the code that parses the contents like to, subject, text of the email and then communicate with the SMTP server, is needed in all the 5 application. The best thing would be, put everything in a dll and do a regsvr32. Boom !!!! the dll can be accessed from all the applications. Life was good. However, maintenance was problem. After a while what if one of the applications wants a slightly different functionality.. you can go with a brand new dll for it.. and may be a while later, another brand new for another application.. each time the underlying code in the application has to be changed to access the new dll, so, the point is it was simply not possible to have different version of same dll on the same computer

Well, then came .NET. Here different versions of the same dll can be present in the same computer. How ? Maintain a local copy of the dll in the project bin folder. You don’t have to register globally. Each project will look into the local bin folder.

OK. What if there is a need to have a machine wide copy .. say for example drivers that connect to SQL server.. well GAC is your buddy.. Global Assembly Cache.  Works same as COM days. The application first looks in the bin folder and then if it doesn’t find the dll, then it looks in the GAC.

Here is how you can use GAC.

·       Use Windows Explorer to drag assemblies into the cache (C:\WINNT\assembly)
·       Use a developer tool called the Gacutil.exe, provided by the .NET Framework SDK. (gacutil -i helloworld.dll).. this tool is not recommended for production deployment. I don’t know why. But Microsoft recommends using Installer tools. 

Assemblies deployed in the global assembly cache must have a strong name. A strong name consists of the assembly's identity — its simple text name, version number, and culture information (if provided) — plus a public key and a digital signature. More on Strong Name

Note   Assemblies placed in the global assembly cache must have the same assembly name and file name (not including the file name extension). For example, an assembly with the assembly name of myAssembly must have a file name of either myAssembly.exe or myAssembly.dll.

Multiple copies of assemblies with the same name but different version information can be maintained in the global assembly cache. Not possible in COM.

Disadvantages: In the COM and GAC, when ever you move code across servers, make sure, you register COM dlls / put assemblies in GAC. You can not use xcopy if GAC is used in your application.