7

私の希望するapp.configは次のようになります。

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

...そして、そのすぐ下に実際のグループとセクションがあります。しかし、私はうまくいくものやより良い提案に満足しています。私は今これに私の願いを下げました:

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

そして、それは問題ないと思います...私が疑問に思っている主なことは、これらのセクションの1つをルートレベルのappSettingsとして置き換えることができるかどうかです...それらを繰り返し、プログラムで構成を追加または作成して保存する必要はありません。ユーザーが環境を選択できるようにしたいだけです。selectイベントによってappSettingsが変更されます...

私が直面している制約の1つは、参照しているデータレイヤーをそのままにしておく必要があるということです。したがって、基本的に、app.configに、現在これらの他のプロジェクトとまったく同じようにアクセスできるようにする必要があります。 ...つまり、ConfigurationManager.AppSettings [afdasdf]

これについて説明が必要な場合はお知らせください...ありがとうございます

4

2 に答える 2

5

よろしければ、ここで自分の質問に答えます。私はそれを実際よりもずっと難しくしていることに気づきました。あなたがしなければならないのはこれだけです:

<?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>

だから私のapp.configの一部があります....残りは同じように簡単です:

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();
    }

config.Saveメソッドを使用して、設定したばかりの設定をすぐにロードすることが明らかに重要であることに注意してください。それ以外は、パス名とセクションタイプだけを決めなければなりませんでした。以下のリンクが最も便利だと思いました。誰かがもっとエレガントな方法を持っているなら、私はそれについて聞いてみたいと思います。

これが私の研究で最大限に活用した場所です

于 2011-08-08T06:33:13.023 に答える
2

展開固有の web.config ファイルを処理する別の方法があります。基本の web.config ファイルに対して、編集コマンドを記述するデプロイメント固有の web.config ファイルを (すべてを繰り返すのではなく) 定義できます。この SO questionの回答をご覧ください。

基本的に、プロジェクトをデプロイするときにベースの web.config ファイルとマージされる web.debug.config および web.release.config ファイルを定義できます。

于 2011-08-02T23:24:28.183 に答える