partial class DaveKehring : IProgrammer { }  RSS 2.0
# Tuesday, April 07, 2009

Don't you just love those days when you come to work full of energy and ideas, ready to have a productive day only to be stumped by a seemingly simple problem that ends up wasting the entire day? Today was one of those days. My only consolation is that this blog post reports the solution, hopefully saving some other poor soul their time and their sanity.

The need was simple. Write out connection strings to the app.config file of an application and encrypt them. Sound easy? Sure. There are lots of examples out there about how to do this, but there's a problem no one seems to mention. You can't just update a config file at runtime and expect those changes to stick. I ran into that problem the other day and solved it here. But there's a more sinister problem. Let me explain.

First, here's what I'm doing:

I have an array of connection strings (as strings) from an external service that I want to add to the app.config file of the current application. I write them out to the <connectionStrings> section and then save the file, taking care to make sure I tell the System.Configuration.ConfigurationManager to refresh the section, meaning it should reload the section from disk the next time it's accessed.

When this happens further along in the program on this line ...

ConnectionStringSettings dbConn = System.Configuration.ConfigurationManager.ConnectionStrings[DefaultDatabaseKey];

... I get this error message:

Unrecognized attribute 'configProtectionProvider'.

I did the usual searches and came up with no solutions. Some folks suggested loading two AppDomains, restarting the app, etc. I can't believe you'd have to restart an app just to pick up configuration changes! After all, I can to this now. The problem comes when I try to encrypt the section.

When you successfully encrpyt a section, a new attribute "configProtectionProvider" is added to the <connectionStrings> element. It appears that when ConfigurationManager tries to reload this section, it can't digest this new attribute.

Okay, let's just get to the solution before I waste even more of my day...

The answer turned out to be simple. Instead of telling ConfigurationManager to refresh the "connectionStrings" section, have it refresh the next level up which would be the <configuration> section itself:

Oh happy day.

 

Tuesday, April 07, 2009 5:03:41 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
Configuration
# Wednesday, April 01, 2009

I was running into a problem when trying to modify the app.config file for an application at runtime. Specifically, I was adding connection strings to the <connectionStrings> section but further along in my code the changes were not visible. However, the configuration file was changed! How could this be? After much head scratching and research, I find the problem.

To change the app.config file I was opening the config file like this:

     System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

I then add or update individual connection strings as need (that code's not important). Finally, I save the changes:

     config.Save(System.Configuration.ConfigurationSaveMode.Modified);

Further on in the code I then attempt to access the connection strings, but instead I use the ConfigurationManager class like this:

     ConnectionStringSettings dbConn = System.Configuration.ConfigurationManager.ConnectionStrings[DefaultDatabaseKey];

The problem is that the ConfigurationManager - which is a static class - already has a stored copy of the configuration file in its internals. The answer is to call RefreshSection("...") after saving changes to the config file:

   config.Save(System.Configuration.ConfigurationSaveMode.Modified);
   System.Configuration.
ConfigurationManager.RefreshSection("connectionStrings");

 

Wednesday, April 01, 2009 11:21:45 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Configuration
# Thursday, November 20, 2008
How to add Intellisense in Visual Studio for custom configuration sections.
Thursday, November 20, 2008 8:57:11 AM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
Configuration
Navigation
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Dave Kehring
Sign In
Statistics
Total Posts: 11
This Year: 0
This Month: 0
This Week: 0
Comments: 2
Themes
Pick a theme:
All Content © 2010, Dave Kehring
DasBlog theme 'Business' created by Christoph De Baene (delarou)