Configuration Files for GAC Assemblies

The other day I was working on a project where I needed to have some configuration settings for an assembly that is installed in the GAC.  I’ve needed this before, but I’ve always given up on it because I always had another way to store those settings.  This time was different because I was writing a GAC assembly that was being used by another GAC assembly that was exposing a COM interface.  Given this situation I didn’t have anywhere else to put the configuration settings.

So, after a little digging and searching on the Internet I came up with this approach for the development machine.

  1. Create the project for the assembly.
  2. Add an app.config file and populate the settings.
  3. Add a .snk file (created by sn –k [filename].snk at a “Visual Studio 2005 Command Prompt”)
  4. Modify the project setting to Sign the Assembly on the Signing tab.
  5. Modify the project settings to add the following build events on the Build tab (See notes below):
    del "C:\WINDOWS\assembly\GAC_MSIL\$(TargetName)\[AssemblyVersion]__[AssemblyPublicKey]\$(TargetFileName).config"
    "$(DevEnvDir)..\..\SDK\v2.0\bin\gacutil" /u "$(TargetName)"

    "$(DevEnvDir)..\..\SDK\v2.0\bin\gacutil" /i "$(TargetPath)"
    copy "$(TargetPath).config" "C:\WINDOWS\assembly\GAC_MSIL\$(TargetName)\ [AssemblyVersion]__[AssemblyPublicKey]\"
  6. Build the project and test the component

Notes on the build step:

First, to find the path of the GAC’d assembly you’ll need to do the following:

  1. Build the assembly without the copy/del .config part of the build events
  2. Open a command window
  3. Go to c:\windows\assembly; type "dir [assembly name]*.* /s"
  4. Copy the directory path and replace the "C:\WINDOWS\assembly\GAC_MSIL\$(TargetName)\ [AssemblyVersion]__[AssemblyPublicKey]\" with that directory
  5. Finally replace the assembly name with $(TargetName).

Once you've done this once and have your assembly version and assembly public key then you'll be able to reuse it in the future.

Second, you have to copy the .config file after you register the file in the GAC and delete the .config file before you unregister the file.

Now, to use it in code you’ll need to load it in the following manner:

       Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

Once you have the Configuration object, then you can access your setting this way(assuming your setting in your config file is <add key="VerboseLogging" value="true"/>):


So there you have it.  It’s relatively straight forward, at least for the development machine.  In a future blog I’ll tell you how to deploy it on a target machine…that is, once I figure it out. :-)

Comments (2) -

  • Hello,

    Did you ever solve the deployment to target machine side for this as I think I have exactly the same problem!

  • Richard

    No, I never had to take this into a production situation.  I think if I had to I would be looking at hooking into some post installation events to copy/move that .config into the directory after DLL has been installed in the GAC.

    Hope that helps.

Comments are closed