3

複数のカスタム構成ファイルを使用してASP.NETC#アプリケーションを構築するのを手伝っています。これらは要素をweb.config使用してファイルの外部に保存し、ファイルの保存によってアプリケーションが再起動しないように設定してください。<configSections><section /></configSections>restartOnExternalChanges="false"

これらの設定を管理するために、さまざまなセクションと値を読み取り、更新し、保存するASPXページを作成しました。プレーンテキストセクションの場合、これはかなりうまく機能します。管理者はアプリの構成セクションにログインし、変更を加えて保存すると、すぐに有効になります。

ただし、他のすべてのファイルと同様に、独自の外部ファイルに1つのセクションがあり、これも暗号化されています。そのセクションが保存されるたびに、IISアプリケーションはセッションを失って再起動します。つまり、ログインした管理者が構成を変更し、アプリのユーザー側にログインしたすべてのエンドユーザーが再度ログインする必要があるため、イライラします。

暗号化された構成セクションが保存されたときにIISアプリケーションの再起動を回避する方法はありますか?

現在の暗号化された構成セクションの処理コードは次のようになります。

public class CredentialsConfig : ConfigurationSection
{
    Configuration Config = null;
    CredentialsConfig Section = null;

    public static CredentialsConfig GetConfig()
    {
        Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
        CredentialsConfig section = config.GetSection("credentials") as CredentialsConfig;

        // Ensure the current file is encrypted
        if (!section.SectionInformation.IsProtected)
        {
            section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            section.SectionInformation.ForceSave = true;
            config.Save();
        }

        return section;
    }

    public CredentialsConfig GetConfigForUpdate()
    {
        Config = WebConfigurationManager.OpenWebConfiguration("~");
        Section = Config.GetSection("credentials") as CredentialsConfig;
        return Section;
    }

    public void SaveConfig()
    {
        Section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
        Section.SectionInformation.ForceSave = true;
        Config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("credentials");
    }

    [ConfigurationProperty("administrator")]
    public AdministratorConfigurationElement Administrator
    {
        get
        {
            return this["administrator"] as AdministratorConfigurationElement;
        }
    }
}

そして、私たちはそれを次のように使用しています:

CredentialsConfig credentialsConfig = new CredentialsConfig();
AdministratorConfigurationElement newConfig = credentialsConfig.GetConfigForUpdate().Administrator;
// setting attributes in the section's <administrator /> element
credentialsConfig.SaveConfig();
// frustrating app restart happens
4

1 に答える 1

5

いいえ。.configファイルへの変更は、常にアプリケーションを再起動する自動トリガーの1つです。

個人的には、一般的に変更されるアイテムを.configファイルからデータベース、レジストリ、または暗号化して保護できる他の場所に移動することをお勧めします。これは問題ではありません。

于 2012-05-31T22:04:53.560 に答える