Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!!)

like image 765
marc_s Avatar asked Apr 11 '09 11:04

marc_s


People also ask

Where is .NET configuration file?

This file is located in the %runtime install path%\Config directory. Machine. config contains configuration settings for machine-wide assembly binding, built-in remoting channels, and ASP.NET.

Does .NET Core has web config?

The web. config file has also been replaced in ASP.NET Core. Configuration itself can now be configured, as part of the application startup procedure described in Startup.


1 Answers

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.

like image 133
Keltex Avatar answered Sep 18 '22 20:09

Keltex