gpt4 book ai didi

.net - 强制 ConfigurationManager 重新加载所有部分

转载 作者:行者123 更新时间:2023-12-04 12:59:53 28 4
gpt4 key购买 nike

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

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

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

    在转移到一个 AppDomain 时,工作流程将更改为:
  • Bootstrapper 构建配置文件。
  • Bootstrapper 将配置文件合并到自己的配置文件中。
  • Bootstrapper 刷新其 ConfigurationManager 缓存。
  • Bootstrapper 在同一个 AppDomain 中启动主应用程序。

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

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

    最佳答案

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

    似乎没有标准的方法来做到这一点。但是,通过访问 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 类的实现,它可能随时中断。

    关于.net - 强制 ConfigurationManager 重新加载所有部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1087539/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com