我正在幫助用多個自定義配置文件構建ASP.NET C#應用程序。我們使用<configSections><section /></configSections>
元素在web.config
文件之外存儲這些文件,確保設置restartOnExternalChanges="false"
,以便文件保存不會重新啓動應用程序。保存加密配置節時防止IIS應用程序重新啓動?
要管理這些設置,我們構建了ASPX頁面來讀取,更新和保存各個部分和值。對於純文本部分,這很有效。管理員登錄到應用程序的配置部分,進行更改,保存並立即生效。
但是我們有一個部分,像其他所有其他文件一樣,也是加密的。只要保存了該部分,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
配置部分是否需要駐留在應用配置文件中,還是可以將它們存儲在另一個單獨的(XML)文件中? –
原始問題提到他們今天存儲在'web.config'文件之外。這是我們如何避免應用程序重新啓動時保存其他未加入的部分。 – aczarnowski