【问题标题】:Forcing ConfigurationManager to reload all sections强制 ConfigurationManager 重新加载所有部分
【发布时间】:2014-01-03 23:22:06
【问题描述】:

我正在编写一个配置系统,其中 app.config 文件是从分布在多个位置的各种配置片段动态构建的。目前系统工作如下:

  1. Bootstrapper 构建配置文件。
  2. Bootstrapper 使用新的配置文件作为配置文件初始化新的 AppDomain。
  3. 因此,新的 AppDomain 被配置为使用新的配置文件并且一切正常。

我们希望摆脱这种多 AppDomain 方法;它增加了一层复杂性,尤其是在涉及非托管库和其他遗留代码时。

在转移到一个 AppDomain 时,工作流程将变为:

  1. Bootstrapper 构建配置文件。
  2. Bootstrapper 将配置文件合并到自己的配置文件中。
  3. Bootstrapper 刷新其 ConfigurationManager 缓存。
  4. Bootstrapper 在同一个 AppDomain 中启动主应用程序。

ConfigurationManager 似乎将部分缓存在内存中。例如,如果我在第 3 步之前读取 AppSettings,我必须调用:ConfigurationManager.RefreshSection("appSettings"); 事实上,我必须确保引导程序使用的任何部分都已刷新。

我能够遍历新配置文件中的所有配置部分并强制刷新它们,但是,这会强制配置管理器加载配置文件中引用的任何程序集。如果可能的话,我想推迟这个。如果有办法使 ConfigurationManager 当前在内存中的内容无效?

【问题讨论】:

    标签: .net configuration configurationmanager configurationsection


    【解决方案1】:

    我知道这个问题是很久以前发布的,但我希望这个答案仍然有用。

    似乎没有标准的方法来做到这一点。但是,通过访问 ConfigurationManager 类的内部字段和类型,我能够列出所有加载的部分。我就是这样做的:

    private static IEnumerable<string> GetLoadedSections()
    {
        // s_configSystem can be null if the ConfigurationManager is not properly loaded. Accessing the AppSettings *should* do the trick.
        var appSettings = ConfigurationManager.AppSettings;
    
        FieldInfo s_configSystemField = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static);
        object s_configSystem = s_configSystemField.GetValue(null);
        FieldInfo _completeConfigRecordField = s_configSystem.GetType().GetField("_completeConfigRecord", BindingFlags.NonPublic | BindingFlags.Instance);
        object _completeConfigRecord = _completeConfigRecordField.GetValue(s_configSystem);
        FieldInfo _sectionRecordsField = _completeConfigRecord.GetType().GetField("_sectionRecords", BindingFlags.NonPublic | BindingFlags.Instance);
        Hashtable _sectionRecords = (Hashtable)_sectionRecordsField.GetValue(_completeConfigRecord);
        return _sectionRecords.Keys.OfType<string>();
    }
    

    “system.diagnostics”部分似乎总是被加载。 “appSettings”部分也已加载,因为我必须访问它才能使其始终如一地工作。

    这适用于我的机器 (.NET 4.5),但由于它依赖于内部的东西,如果 Microsoft 决定更改 ConfigurationManager 类的实现,它可能随时中断。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-06
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多