Thursday, April 28, 2011

Read .NET configuration from database

The .NET 2.0 and up configuration system is quite powerful and extensible - as long as you don't want to change the fact it all comes from XML files in the filesystem.

In my requirement, I cannot change files since my app runs in a managed environment outside my reach - but I could change the SQL Server database.

So I am looking at storing configuration files or sections in a SQL table - but how can I tie the .NET 2.0 configuration system into this??

Is there a way to write a "custom config provider" that will read its config sections not from a *.config file in the file system, but from a table in the SQL database??

I've been looking at creating my own custom ConfigurationSection or ConfigurationElement, or even a custom Configuration per se - but it seems I always end up back at the point that I can extend the config-system in the filesystem as much as I like, but I can't make it read my XML fragments from a database table.....

What am I missing? Has someone done this already and care to explain / share?

Thanks! Marc

PS: I also tried to just read the config XML into a string, and then deserializing it into the appropriate e.g. ServiceModelConfigSection - that doesn't work, unfortunately, because the ConfigSection base class somehow doesn't implement a method that is required for it to be XML serializable ........ (YIKES!!!)

From stackoverflow
  • There's an article here that talks about doing what you are talking about:

    http://www.wrox.com/WileyCDA/Section/Redirecting-Configuration-with-a-Custom-Provider.id-291932.html

    In summary what they do is create a derived version of ProtectedConfigurationProvider, which is typically used to encrypt .config files. In the Decrypt method, instead of decrypting the configuration information, it's retrieved from a database.

    marc_s : +1 - thanks, very interesting! Can't quite make sense of the "ProtectedConfigurationProvider" as a base class just yet.... but I'll give it a try!
    marc_s : Hi Keltex - it works - mostly - but it's a pretty awful HACK in my opinion..... too bad there's no "proper", nice, official way to directly plug in config providers into the .NET config system.....
    Keltex : @marc_s I agree. But it is a work-around.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.