Monday, November 5, 2007

Working with .gac files

We already know how to get the list of assemblies registered in the .Net Compact Framework GAC, and use the cgacutil.exe tool to register / unregister a specific assembly. Actually we can do it programmatically launching the cgacutil.exe tool from our application. But there is another way to register assemblies in the .Net CF GAC. This way is through .gac files.

Let's say we have two assemblies implementing our custom UI controls:

  • \MyUI\MyCustomControls.dll
  • \MyUI\MyExtendedControls.dll

and we've several apps using them. It makes sense register them in the GAC. We should create a new "MyUI.gac" file in the "\windows" folder with the following content:


Remember, both assemblies should be strong-named in order to be registered in the GAC.

When we start a .Net CF application, the cgacutil is executed looking for .gac files in the "\windows" folder and all the assemblies listed on these .gac files are registered.

What if you change the content of your .gac file? Well, the corresponding changes will be made to the GAC: if you delete an assembly from the .gac file it'll be unregistered from the GAC and if you add a new one it'll be registered in the GAC.

If you delete the .gac file, all the assemblies will be removed from the GAC.

How it works

When .Net CF find a new .gac file, it creates a new registry key in the following location:


Continuing with our example, it will create the following key:


containing values for each assembly registered by the .gac file, and a timestamp.

Additionally, it creates references for each assembly in the registry key:


Following our example, it will create two registry keys:

[HKEY_LOCAL_MACHINE\Software\Microsoft\.NETCompactFramework\Installer\Assemblies\Reference\MyCustomControls, Version=, Culture=neutral, PublicKeyToken=F170A8B616315F91]

[HKEY_LOCAL_MACHINE\Software\Microsoft\.NETCompactFramework\Installer\Assemblies\Reference\MyExtendedControls, Version=, Culture=neutral, PublicKeyToken=DB19C2BE9EEAE55A]

and a dword value in each key, named "\windows\MyUI.gac".

Some tips

When the .gac is processed, and the assemblies are registered in the GAC, the source files are deleted from the file system. Following our example, both files (MyControls.dll and MyExtendedControls.dll) will be deleted. This is very important for saving space.

Renaming a .gac file can be tricky and it's not a good practice. It's recommendable to delete the old .gac file first and check all the information has been removed from the registry, and then add a new .gac file with the new filename.

No comments: