ConfigurationManager.AppSettings [Key]每次都从web.config文件中读取吗?

时间:2012-11-13 08:48:01

标签: c# asp.net web-config configurationmanager

我只是想知道ConfigurationManager.AppSettings [Key]是如何工作的?

每次需要密钥时,它是否从物理文件中读取?

如果是这样,我应该在缓存中读取web.config的所有应用程序设置,然后从中读取吗?

或者ASP.NET或IIS只在application_startup加载web.config文件一次。

如何验证每次读取是否访问物理文件?

如果我更改了web.config,IIS将重新启动我的应用程序,因此无法以这种方式进行验证。

谢谢,

3 个答案:

答案 0 :(得分:83)

首次访问属性时会缓存它,因此每次请求值时它都不会从物理文件中读取。这就是为什么有必要重新启动Windows应用程序(或Refresh配置)以获取最新值,以及为什么在编辑web.config时ASP.Net应用程序会自动重新启动。回答How to prevent an ASP.NET application restarting when the web.config is modified中的参考文献讨论了为什么ASP.Net是硬连接重启的。

我们可以使用ILSpy验证这一点并查看System.Configuration的内部结构:

public static NameValueCollection AppSettings
{
    get
    {
        object section = ConfigurationManager.GetSection("appSettings");
        if (section == null || !(section is NameValueCollection))
        {
            throw new ConfigurationErrorsException(SR.GetString("Config_appsettings_declaration_invalid"));
        }
        return (NameValueCollection)section;
    }
}

首先,这确实看起来每次都会得到该部分。看看GetSection:

public static object GetSection(string sectionName)
{
    if (string.IsNullOrEmpty(sectionName))
    {
        return null;
    }
    ConfigurationManager.PrepareConfigSystem();
    return ConfigurationManager.s_configSystem.GetSection(sectionName);
}

这里的关键线是PrepareConfigSystem()方法;这会初始化ConfigurationManager持有的IInternalConfigSystem字段的实例 - 具体类型为ClientConfigurationSystem

作为此加载的一部分,实例化Configuration类的实例。该类实际上是配置文件的对象表示,并且似乎由ClientConfigurationSystem的ClientConfigurationHost属性保存在静态字段中 - 因此它被缓存。

您可以通过执行以下操作(在Windows窗体或WPF应用程序中)凭经验测试:

  1. 启动您的应用
  2. 访问app.config中的值
  3. 对app.config进行更改
  4. 检查新值是否存在
  5. 致电ConfigurationManager.RefreshSection("appSettings")
  6. 检查新值是否存在。
  7. 事实上,如果我只是阅读RefreshSection方法的评论,我本可以节省一些时间: - )

    /// <summary>Refreshes the named section so the next time that it is retrieved it will be re-read from disk.</summary>
    /// <param name="sectionName">The configuration section name or the configuration path and section name of the section to refresh.</param>
    

答案 1 :(得分:7)

简单的答案是否定的,它并不总是从文件中读取它。有些人建议如果文件被更改,那么IIS会执行重启但不总是!如果您想保证从文件而不是缓存中读取最新值,则需要调用以下内容:

ConfigurationManager.RefreshSection("appSettings");
string fromFile = ConfigurationManager.AppSettings.Get(key) ?? string.Empty;

我在代码中使用的一个例子:

/// ======================================================================================
/// <summary>
/// Refreshes the settings from disk and returns the specific setting so guarantees the
/// value is up to date at the expense of disk I/O.
/// </summary>
/// <param name="key">The setting key to return.</param>
/// <remarks>This method does involve disk I/O so should not be used in loops etc.</remarks>
/// <returns>The setting value or an empty string if not found.</returns>
/// ======================================================================================
private string RefreshFromDiskAndGetSetting(string key)
{
    // Always read from the disk to get the latest setting, this will add some overhead but
    // because this is done so infrequently it shouldn't cause any real performance issues
    ConfigurationManager.RefreshSection("appSettings");
    return GetCachedSetting(key);
}

/// ======================================================================================
/// <summary>
/// Retrieves the setting from cache so CANNOT guarantees the value is up to date but
/// does not involve disk I/O so can be called frequently.
/// </summary>
/// <param name="key">The setting key to return.</param>
/// <remarks>This method cannot guarantee the setting is up to date.</remarks>
/// <returns>The setting value or an empty string if not found.</returns>
/// ======================================================================================
private string GetCachedSetting(string key)
{
    return ConfigurationManager.AppSettings.Get(key) ?? string.Empty;
}

这使您可以非常轻松地选择(以及在阅读代码时)是否每次都获得最新值,或者如果您不希望值从应用程序启动时更改。

答案 2 :(得分:1)

var file =
            new FileInfo(@"\\MyConfigFilePath\Web.config");

        DateTime first  = file.LastAccessTime;

        string fn = ConfigurationManager.AppSettings["FirstName"];
        Thread.Sleep(2000);

        DateTime second = file.LastAccessTime;

        string sn = ConfigurationManager.AppSettings["Surname"];
        Thread.Sleep(2000);

        DateTime third = file.LastAccessTime;

All显示相同的LastAccessTime,这意味着它在启动时被缓存。

        string fn1 = ConfigurationManager.AppSettings["FirstName"];
        Thread.Sleep(2000);

        DateTime fourth = file.LastAccessTime;