开发者

Can I configure a .NET application to read settings from an SQL database?

I want my app.config (or my web.config for th开发者_如何学Goat matter) to read from an SQL database instead of an XML file. How can I do that?


You can't. The .NET configuration system is only file based. Anyway, how would your program know how to connect to the database ? Of course, you can put some settings in the database, but you program will still need a config file to get the DB connection string (unless you hard-code it, which is of course not recommended)


You would need to either hard-code or configure, by app.config or other means, connection information in order to connect to the database.

Then, as far as I've been able to find out, there is no way to make use of the .net framework's configuration capabilities except via files. But it is possible to load a particular file, by the ConfigurationManager.OpenMappedExeConfiguration method.

This should open up the door, since you could store the config file in a varbinary(max) field in SQL server. As your app(s) connect to the sql server db, they need only extract the configuration, save it to a temporary file, and use the above method to load it.

I am going to test this, as I also have reasons to stuff this into a database. First, more than one app is going to depend on the same configuration, and so I want to have only one. Not just for ease of maintenance, but also for ensuring the apps configurations are in sync, as it is difficult to predict the behavior if they both have seemingly valid but different configurations (say app 1 connects to a DB, and app 2 connects to a different instance of the DB, i.e. the same schema but a different database).

The second reason for me is that the system works across Windows/Unix boundaries and we have no means to do directory-based authentication towards the Unix world. I can't store credentials in plain text. And though I could encrypt the configuration, that opens up a can of worms on the operational side: Who can decrypt it? Who can create a new config (encrypted) for the app to use? Who can decide who can do these things?? It just gets worse the more you think about it. :) By putting this stuff in an MSSQL db the problems go away. We can use windows authentication to control who can read/modify the db, and encryption becomes a DBA matter, transparent to my applications.

Hopefully this extract-to-file and OpenMappedExeConfiguration approach will achieve these goals.


Your application will need to know how to connect to the database, so your config file will, at least, need a relevant connection string. However, once you have that in place, you can use ADO.NET to connect to the database and select whatever further config you desire.


I can't see any way you can. You can easily replace the concept of app.config settings with fields stored in a custom database - you just need to write your own configuration class that reads the values from the DB and then makes them accessible as static properties or as Dictionary or similar.

However, web.config is different - asp.net expects to read it's configuration values from a file called web.config. If you really, really want to store values meant for web.config in a database then the best you could do was write an app that would generate the XML file and write it out. But I would really question why on earth you would want to?


There is a plethora for GREAT reasons to store config values in a database including the ability to create hierarchical overrides dynamical depending on the runtime environment, such as the user (NOT their profile controls), host machine or other runtime characteristics. For example, controlling a target machine from a control room or such. You sure don't want to go find that box and change is config values in some file. Enterprise control is the game. (Hence why MS provided a rudimentary capability in .Core via the manager)

Problem is.. not using core and also a robust set of filtering/hierarchical support for a given configuration object.

There are a few nugets available but they are still extremely basic (from what I can find so far). I've written my own so I can say this.Configure() or Configure(someobject) but that's not preferable.


Three words: why, oh why?

App config files are there for a reason. Introducing a database as a dependency for settings required for the app to even run, is not a good idea - if it even could be done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜