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
Navigation
Archive
<April 2009>
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 2014
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 © 2014, Dave Kehring
DasBlog theme 'Business' created by Christoph De Baene (delarou)