【问题标题】:ConfigurationManager.RefreshSection not working in c# windows serviceConfigurationManager.RefreshSection 在 c# windows 服务中不起作用
【发布时间】:2014-10-13 10:49:09
【问题描述】:

我有两个项目,一个基本上是 Windows 服务,第二个是我正在进行业务处理的类项目。我的 App.config 文件在 Windows 服务项目中,在我正在使用的 Class 项目中

ConfigurationManager.RefreshSection("appsettings");           
string scheduledTime = ConfigurationManager.AppSettings["ScheduleTime"];

此设置在我的配置文件的 appsettings 部分中

我正在使用 RefreshSection 并且还在 Windows 服务项目中更新我的 app.config 值,但在我的类项目中运行时它没有得到更新。

这有什么问题?

【问题讨论】:

    标签: c# windows-services app-config


    【解决方案1】:

    我自己遇到了一些类似的问题,但是我确实遇到了 AppSettings 的一些问题。试一试:

    ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
    

    我认为 appsettings 应该是 appSettings

    【讨论】:

    • 实际上这对我有用。问题是我需要按名称提取外部 DLL 的配置。那个被缓存了,默认调用失败了。通过名称调用它后,它就像一个魅力。谢谢!
    【解决方案2】:

    如果您想获取最新值,请使用此代码:

    var appSettings = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetEntryAssembly().Location).AppSettings;
    var mySetting = appSettings.Settings["keyOfSetting"].Value;
    

    记住每次需要最新值时,必须在同一个地方使用两行代码!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-04
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多