从数据库读取 .NET 配置 [英] Read .NET configuration from database
问题描述
.NET 2.0 及更高版本的配置系统非常强大且可扩展 - 只要您不想改变它都来自文件系统中的 XML 文件的事实.
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.
根据我的要求,我无法更改文件,因为我的应用程序在我无法触及的托管环境中运行 - 但我可以更改 SQL Server 数据库.
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.
所以我正在考虑将配置文件或部分存储在 SQL 表中 - 但是我如何将 .NET 2.0 配置系统与此相关联??
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??
有没有办法编写一个自定义配置提供程序",它不会从文件系统中的 *.config 文件中读取其配置部分,而是从 SQL 数据库中的表中读取?
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??
我一直在考虑创建我自己的自定义 ConfigurationSection 或 ConfigurationElement,甚至是自定义配置本身 - 但似乎我总是回到我可以在文件系统中尽可能多地扩展配置系统的点我喜欢,但我不能让它从数据库表中读取我的 XML 片段.....
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?
谢谢!马克
PS:我还尝试将配置 XML 读入字符串,然后将其反序列化为适当的例如ServiceModelConfigSection - 不幸的是,这不起作用,因为 ConfigSection 基类以某种方式没有实现 XML 可序列化所需的方法........(YIKES!!!)
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!!!)
推荐答案
这里有一篇文章讨论了做你在说什么:
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
总而言之,他们所做的是创建 ProtectedConfigurationProvider 的派生版本,该版本通常用于加密 .config 文件.在 Decrypt 方法中,不是解密配置信息,而是从数据库中检索.
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.
这篇关于从数据库读取 .NET 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!