views:

1246

answers:

3

I am trying to override the ApplyConfiguration method in my custom ServiceHost to read the configuration from a database instead of app.config. Ideally I would want 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 a temp app.config file?

+2  A: 

What about using:

System.Configuration.ConfigurationManager.OpenExeConfiguration(string exePath)

That should let you open an arbitrary app.config file.

Joe
A: 

Despite you're not wanting to write the temp config file, the best way to do this is to host your service(s) in a separate AppDomain.

Before creating your AppDomain, grab the config from the database and write it to the file system, then when you create your AppDomain point it at the temp config file you retrieved from the database as it's config source.

Of course the config in the database would either have to be a full app.config file, or you'd have to merge it with some kind of template config file that had any other non-serviceModel configuration bits in it for the rest of your app.

Implementing it in this way is quite a neat solution, and works well (have used it before).

Mark Allanson
feels very hacky!
Cheeso
A: 

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

The ServiceHost has an ApplyConfiguration method that you can override. You can load config from wherever you like.

See here for a relevant article outlining the technique.

Cheeso