Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.config: how do I make a nested customSection called appSettings be the ConfigurationManager.AppSettings

my desired app.config would be like this:

<configSections>
        <sectionGroup name="QA_Environment">
            <section name="databases" type="System.Configuration.NameValueSectionHandler"/>
            <section name="storageSystems" type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
        <sectionGroup name="Production_Environment">
            <section name="databases" type="System.Configuration.NameValueSectionHandler"/>
            <section name="storageSystems" type="System.Configuration.NameValueSectionHandler"/>
        </sectionGroup>
    </configSections>

...and then I've got the actual groups and sections right below that. But I'd be happy with whatever works or better suggestions though. I've now lowered my wishes to this:

    <configSections>
    <sectionGroup name="QA_Environment">
        <section name="appSettings" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
    <sectionGroup name="Production_Environment">
        <section name="appSettings" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
</configSections>

And I guess that's fine...The main thing I'm wondering about is if I can substitute one of these sections as the root level appSettings...without iterating through them and programmatically adding or creating the config and saving it. I just want the user to be able to select an environment, the select event will change the appSettings...

One constraint that I'm facing is that the data layer I'm referencing needs to remain the same as it is....so I basically need to get my app.config to be accessible exactly like it is currently from these other projects...that is ConfigurationManager.AppSettings[afdasdf]

Let me know if this needs any clarification...thanks

like image 973
J Benjamin Avatar asked Aug 02 '11 22:08

J Benjamin


People also ask

What is the use of ConfigurationManager AppSettings?

Store custom information from a configuration file - C#

What is ConfigurationManager configuration?

The ConfigurationManager class enables you to access machine, application, and user configuration information. This class replaces the ConfigurationSettings class, which is deprecated. For web applications, use the WebConfigurationManager class.

Why AppSettings keys in web config are not case sensitive?

Why appsettings keys (in web. config) are not case sensitive ? The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default.


2 Answers

I'll go ahead and answer my own question here if that's alright. I found that I was making it much harder than it really is. All you have to do is this:

<?xml version="1.0" encoding="utf-8"?>

<configSections>
    <sectionGroup name="Environment">
        <sectionGroup name="QA">
            <section name="databases" type="System.Configuration.DictionarySectionHandler"/>
            <section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/>
        </sectionGroup>
        <sectionGroup name="PROD">
            <section name="databases" type="System.Configuration.DictionarySectionHandler"/>
            <section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/>
        </sectionGroup>
    </sectionGroup>
</configSections>

<Environment>
    <QA>
        <databases>
        </databases>
        <storageSystems>
        </storageSystems>
    </QA>

    <PROD>
        <databases>
        </databases>
        <storageSystems>
        </storageSystems>
    </PROD>
</Environment>

So there's part of my app.config....the rest is just as easy:

private void GetConfigurationSettings(TargetEnvironments targetEnvironment)
    {
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        var databases = new Hashtable();
        var storageSystems = new Hashtable();

        switch (targetEnvironment)
        {
            case TargetEnvironments.QA:
                databases = (Hashtable)ConfigurationManager.GetSection("Environment/QA/databases");
                storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/QA/storageSystems");
                break;
            case TargetEnvironments.PROD:
                databases = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/databases");
                storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/storageSystems");
                break;
        }

        foreach (string key in databases.Keys) { config.AppSettings.Settings.Add(key, databases[key].ToString()); }
        foreach (string key in storageSystems.Keys) { config.AppSettings.Settings.Add(key, storageSystems[key].ToString()); }

        config.Save(ConfigurationSaveMode.Modified);

        ConfigurationManager.RefreshSection("appSettings");

        UpdateCollections();
    }

Note the obviously important use of the config.Save method to immediately load the settings that you've just set. Other than that, it's really just the path names and the section type that I had to kinda decide on. I found the link below to be the most useful. If someone's got a more elegant way I'd be interested to hear about it.

Here's the place I got the most out of in my research

like image 104
J Benjamin Avatar answered Oct 20 '22 00:10

J Benjamin


There is another way to deal with deployment-specific web.config files. You can define deployment-specific web.config files that describe edit commands to the base web.config file (rather than repeating everything). Have a look at the answers for this SO question.

Basically, you can define a web.debug.config and a web.release.config file that gets merged with the base web.config file when you deploy your projects.

like image 22
Philipp Schmid Avatar answered Oct 19 '22 23:10

Philipp Schmid