问题
We are running multiple instances of our service (implemented as a .exe file) on different machines in our data center. However, we would like to store configuration parameters for this .exe file in a central location (as opposed to being in the .exe.config per machine).
To do this, we are planning to move the content of the .exe.config files to an attribute in a central repository (Active Directory or SQL) and make the .exe's running on all machines read from this repository.
I would like reuse all the existing classes used for reading/writing configuration information (using ConfigurationManager, ConfigurationProperty etc., basically using the entire .NET Configuration framework) - however, instead of loading it from the .exe.config file (which it does by default - I want it to load it from a string value of the XML blob that I will retrieve from that central repository.
I was trying to find ways to do it using the ConfigurationManager API but couldn't find anything that could load a Configuration Section from a string. Any pointers?
回答1:
I suppose you could write your own ConfigurationSection that will then load the real config data from your central repository.
Alternatively, do you have some sort of centralized deployment system in place for installing software on your various servers? How does your service get deployed? Rather than adding complexity to your software you should look for existing tools that already solve your problem.
回答2:
Although I doubt this is what it was intended for, we've done this in our project using ProtectedConfigurationProvider. Basically, this class is supposed to allow you to encrypt your configuration file but we simply have the Decrypt method connect to our database and return the configuration information. See this for notes on implementing. This felt a little hacky, but we couldn't find any other way without completely rewriting all of the configuration parsing logic.
来源:https://stackoverflow.com/questions/9779962/using-net-configurationmanager-to-read-settings-from-a-string-instead-of-an-app