是否ConfigurationManager.AppSettings [关键词]每次从web.config文件中读取? [英] Does ConfigurationManager.AppSettings[Key] read from the web.config file each time?

查看:980
本文介绍了是否ConfigurationManager.AppSettings [关键词]每次从web.config文件中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何ConfigurationManager.AppSettings [关键]的作品?

I'm just wondering how the ConfigurationManager.AppSettings[Key] works?

它与物理文件每次读我需要一个密钥?

Does it read from the physical file each time I need a key?

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

If so, should I be reading all the app settings of my web.config in a cache and then read from it?

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

Or ASP.NET or IIS loads the web.config file at application_startup and only once.

如何验证物理文件是否被访问的每个读?

How to verify whether the physical file is accessed by each read?

如果我改变web.config文件,重新启动IIS我的应用程序因此无法验证这种方式。

If I change the web.config, IIS restarts my application so can't verify it that way.

谢谢,

推荐答案

据被缓存,在财产的首次访问,因此它不会从物理文件中的每个你问一个值时读取。这就是为什么有必要重新启动的Windows应用程序(或<一个href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx\">Refresh在配置),以获得最新的价值,为什么一个ASP.Net应用程序会自动重新启动时,您编辑的web.config。为什么ASP.Net是硬连接,重新启动在回答引用讨论如何prevent ASP.NET应用程序重新启动时在web.config被修改

It gets cached, on first access of a property, so it does not read from the physical file each time you ask for a value. This is why it is necessary to restart an Windows app (or Refresh the config) to get the latest value, and why an ASP.Net app automatically restarts when you edit web.config. Why ASP.Net is hard wired to restart is discussed in the references in the answer How to prevent an ASP.NET application restarting when the web.config is modified.

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

We can verify this using ILSpy and looking at the internals of 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:

At first, this does indeed look like it will get the section every time. Looking at 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

The critical line here is the PrepareConfigSystem() method; this initializes an instance of the IInternalConfigSystem field held by the ConfigurationManager - the concrete type is ClientConfigurationSystem

作为该负载的一部分,中的<一个实例href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx\">Configuration类实例化。这个类是有效的配置文件的对象重新presentation,并且似乎是由ClientConfigurationSystem的ClientConfigurationHost属性在静电场举行 - 因此,它被缓存

As part of this load, an instance of the Configuration class is instantiated. This class is effectively an object representation of the config file, and appears to be held by the ClientConfigurationSystem's ClientConfigurationHost property in a static field - hence it is cached.

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

You could test this empirically by doing the following (in a Windows Form or WPF app):


  1. 启动你的App高达

  2. 访问中的app.config
  3. 的值
  4. 请更改的app.config

  5. 请检查新值是否为present

  6. 呼叫 ConfigurationManager.RefreshSection(的appSettings)

  7. 检查,看是否新的值是present。

其实,我可以救自己一些时间,如果我刚刚读了<一个评论href=\"http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.refreshsection.aspx\">RefreshSection方法: - )

In fact, I could have saved myself some time if I'd just read the comment on the RefreshSection method :-)

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

这篇关于是否ConfigurationManager.AppSettings [关键词]每次从web.config文件中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆