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

半城伤御伤魂 提交于 2019-12-30 03:30:06

问题


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?


回答1:


What about using:

System.Configuration.ConfigurationManager.OpenExeConfiguration(string exePath)

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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/771814/how-to-read-app-config-from-a-custom-location-i-e-from-a-database-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!