当前位置:首页 > Windows程序 > 正文

解决Windows服务修改配置文件后必须重启的问题

2021-05-25 Windows程序

解决方法:读取配置文件前先刷新文件

// 刷新命名节,,在下次检索它时将从磁盘重新读取它。

ConfigurationManager.RefreshSection("appSettings");

recordCount = Math.Abs(int.Parse(ConfigurationManager.AppSettings["RecordCount"]));

 

分析:.Net读取配置文件一般是从缓存中读取,修改配置文件后并不能影响缓存中的值,而读取前刷新文件,.Net会从硬盘文件中读取而不是从缓存中读取配置文件值

 

 

性能比较:

前置条件:app.config中有六个配置项

 

调用RefreshSection 后读取10000次:

var sw = new Stopwatch();

sw.Start();

 

for (int i = 0; i < 10000; i++)

{

ConfigurationManager.RefreshSection("appSettings");

object value = ConfigurationManager.AppSettings["RedisServer"];

}

TimeSpan ts = sw.Elapsed;

string elapsedTime = String.Format("{0:00}:时 {1:00}:分 {2:00}:秒:{3:00}:毫秒",

 ts.Hours, ts.Minutes, ts.Seconds,

 ts.Milliseconds / 10);

//00:时 00:分 07:秒:43:毫秒

string kk = elapsedTime;

 

不调用RefreshSection 后读取10000次:

var sw = new Stopwatch();

sw.Start();

 

for (int i = 0; i < 10000; i++)

{

object value = ConfigurationManager.AppSettings["RedisServer"];

}

 

TimeSpan ts = sw.Elapsed;

string elapsedTime = String.Format("{0:00}:时 {1:00}:分 {2:00}:秒:{3:00}:毫秒",

 ts.Hours, ts.Minutes, ts.Seconds,

 ts.Milliseconds / 10);

//00:时 00:分 00:秒:01:毫秒

string kk = elapsedTime;

 

 

结论:

使用RefreshSection先刷新文件在读取对性能影响还是很大的

解决Windows服务修改配置文件后必须重启的问题

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71126.html