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. :-)

"Kick the Designer in the Nuts" Day...

Welcome to my blog.  I want to start out by setting a base line with something that I have often mentioned to the people I work with.  It’s a concept called “Kick the Designer in the Nuts” Day.

Let me explain what “Kick the Designer in the Nuts” Day is. It’s a day that at any Microsoft sponsored programming conference where you get to argue with the guy that designed some part of the .NET Framework or SharePoint or whatever other Microsoft technology that you have to use every day that is a pain in the butt to do.  You get to tell the designer why what he decided causes you misery every day (or almost every day).  He gets to tell you why he made that decision that he made.  When you are done the debate is judged by three or four MVP’s.  If you win the argument, then you get to kick the designer in the nuts…but if you lose…well then the designer gets to kick you in the "jimmy".

I know, it’s not politically correct at all.  I know I’m also being sexist by assuming that no woman has ever designed anything that drives me crazy.  There are a couple of faults of mine that I’m working on.

The reason that I’m starting off my blog with this story is two reasons. 1) I’ll reference it from time to time in future blogs and I’ll want you to know what I mean by this day.  2)  I’m designing stuff at my current job in which the people that I work with will want to have a day like this too.

Thanks for reading this whole thing and hopefully the future the posts will be technically useful.