【问题标题】:ConfigurationManager.RefreshSection("appSettings") has no effectConfigurationManager.RefreshSection("appSettings") 没有效果
【发布时间】:2016-03-30 02:54:55
【问题描述】:

背景: 我正在使用 Timer 在 Windows 服务中定期做一些工作。我希望 Timer 在运行时可配置。我唯一能做的就是在启动时对其进行配置。

我的解决方案: 我正在使用 app.config 来配置计时器的开始时间和周期:

  <appSettings>
    <add key="StartTime" value="14:40:00"/>
    <add key="Period" value="24:00:00"/>
  </appSettings>

我正在使用 FileSystemWatcher 来通知配置文件上的文件写入(将是 AppName.exe.config)

    public ConfigWatcher(params object[] args)
    {
        configurationChangedListeners = new List<INotifyConfigurationChanged>();

        string assemblyDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
        NotifyFilters notifyFilters = NotifyFilters.LastWrite;
        _fileSystemWatcher = new FileSystemWatcher()
        {
            Path = assemblyDirectory,
            NotifyFilter = notifyFilters,
            Filter = "*.config"
        };
        _fileSystemWatcher.Changed += OnChanged;
        _fileSystemWatcher.EnableRaisingEvents = true;

        if (args != null)
        {
            foreach (var arg in args)
            {
                AddListener(arg);
            }
        }
    }

    private void OnChanged(object source, System.IO.FileSystemEventArgs e)
    {
        try
        {
            _fileSystemWatcher.EnableRaisingEvents = false;
            ConfigurationManager.RefreshSection("appSettings");
            foreach (var listener in configurationChangedListeners)
            {
                listener.NotifyConfigurationChanged();
            }
        }
        finally
        {
            _fileSystemWatcher.EnableRaisingEvents = true;
        }
    }

最后,每个监听器都会像这样得到它的配置:

public void NotifyConfigurationChanged()
{
    string strKeyName = "StartTime";
    string startTime = ConfigurationManager.AppSettings[strKeyName];
    // ...
}

还有问题: - 当我编辑文件时,文件观察器会触发事件,但是当我尝试获取新的 AppSettings 时,我正在读取旧值(从服务启动时开始)

奇怪的是,这个设置在某些时候有效,然后它没有(据我所知,没有更改代码)。

非常感谢任何帮助/建议。

【问题讨论】:

标签: c#


【解决方案1】:

问题的答案(经过大量搜索 :D)是使用 OpenExeConfiguration 而不是直接访问 AppSettings。据我了解,刷新后需要再次打开配置:

var appSettings = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location).AppSettings;
string setting = appSettings.Settings[strKeyName].Value;

我很肯定第一个变体在我第一次尝试时在某种情况下有效......

【讨论】:

    【解决方案2】:

    如果您在 VS 中启动应用程序,请确保您编辑了正确的配置文件。我花了几个小时来抓住它。看:

    在调试时执行 (app_name).vshost.exe,如果要更新任何键,还必须修改 (app_name).vshost.exe.config!

    其次,sectionName 是 CaseSeNsItIvE))

    也许对某人会有所帮助。

    【讨论】:

    • 高度相关且乐于助人;确保你编辑了正确的配置文件!
    【解决方案3】:

    这是一个简单的答案:

    private static ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
    private static Configuration _config;                                               
    private static KeyValueConfigurationCollection _appSettings = null;               
    
    configMap.ExeConfigFilename = @"\Path\To\App.Config";
    _config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    _appSettings = _config.AppSettings.Settings;
    
    // Do your thing
    
    _config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection(_config.AppSettings.SectionInformation.Name);
    

    .Save 可以解决问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-30
      • 2018-05-21
      • 2018-07-28
      • 2018-05-15
      • 1970-01-01
      • 2011-10-01
      • 2015-05-31
      • 2020-10-19
      相关资源
      最近更新 更多