How to read app.config from a user location, i.e. from a database in .NET.

I am trying to override the ApplyConfiguration method in my custom ServiceHost to read the configuration from the database, not app.config. Ideally, I would like to be able to do something like this:

Configuration config = GetConfigFromMyDatabase (...);

ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup (config);

Is there any way to do this without writing the temp app.config file?

+4
source share
3 answers

How about using:

System.Configuration.ConfigurationManager.OpenExeConfiguration(string exePath) 

This should allow you to open an arbitrary app.config file.

+4
source

You do not need a separate AppDomain if you are writing a custom ServiceHost.

ServiceHost has an ApplyConfiguration method that you can override. You can download the configuration from anywhere you want.

See here for a related technique article.

+1
source

Although you do not want to write the temp configuration file, the best way to do this is to host your services in a separate AppDomain.

Before creating the AppDomain, take the configuration from the database and write it to the file system, and then when creating the AppDomain, specify it in the temp configuration file that you extracted from the database as the configuration source.

Of course, the config in the database should either be a complete app.config file, or you would need to combine it with some kind of template configuration file, in which there would be some other non-serviceModel configuration bits in it for the rest of your applications.

Implementing it this way is a pretty neat solution and works well (used it before).

-1
source

All Articles