Change the configuration file (app.config) of the windows service without restarting

This problem was encountered in the previous stage of writing the windows service. Originally, when writing the value of a configuration in the configuration file, I usually write a static method like the following to get it:

   1:          /// <summary>
   2:           /// Get the number of records processed each time
   3:          /// </summary>
   4:          /// <returns></returns>
   5:          private static int GetRecordCount()
   6:          {
   7:              int recordCount = 10000;
   8:              try
   9:              {
  10:                  recordCount = Math.Abs(int.Parse(ConfigurationManager.AppSettings["RecordCount"]));
  11:                  if (recordCount==0)
  12:                  {
  13:                      recordCount = 10000;
  14:                  }
  15:              }
  16:              catch 
  17:              {
  18:                  recordCount = 10000;
  19:              }
  20:              return recordCount;
  21:          }

RecordCount represents the number of records read from the database each time. When the service was written and tested after installation, this parameter was changed several times. At first, its default value was 10000, and later changed to 500, 100, 50, and 10, respectively. But after the change, I had to restart the service to make the configuration file work. After several tossings, I felt the experience was too bad. Internet search and found this one: the Do you have have to restart Windows Service A Change at The IF you the app.config?  In the original problem, a man gives the answer is through the ConfigurationManager. RefreshSection refresh a configuration node, we get There is no need to restart the service when configuring the values:

        /// <summary>
        /// 获取每次处理记录数
        /// </summary>
        /// <returns></returns>
        private static int GetRecordCount()
        {
            int recordCount = 10000;
            try
            {
                ConfigurationManager.RefreshSection("appSettings");// 刷新命名节,在下次检索它时将从磁盘重新读取它。
                recordCount = Math.Abs(int.Parse(ConfigurationManager.AppSettings["RecordCount"]));
                if (recordCount == 0)
                {
                    recordCount = 10000;
                }
            }
            catch
            {
                recordCount = 10000;
            }
            return recordCount;
        }

After testing, this is indeed the case.

你可能会问,为什么加了那一行ConfigurationManager.RefreshSection(命名节点);就可以了呢?

查看MSDN,解释是这样的:“刷新命名节,这样在下次检索它时将从磁盘重新读取”。下面是VS里的函数说明:

        //
        // 摘要:
        //     刷新命名节,这样在下次检索它时将从磁盘重新读取它。
        //
        // 参数:
        //   sectionName:
        //     要刷新的节的配置节名称或配置路径和节名称。
        public static void RefreshSection(string sectionName);

原来,更改配置文件之后,应用程序读取配置的顺序不是从物理文件读取,而是从其缓存中读取(ConfigurationManager.RefreshSection方法在不影响其他节的前提下使指定配置节的缓存失效),必须强制刷新配置文件,才能读取到更改后的配置节信息 。

毫无疑问,和正常的读取配置文件节点方法相比,这个读取配置的方法在性能上应该有点影响,至于损失多少,会不会造成性能瓶颈,你懂的。

最后,从MSDN的例子我们也可以看出来,这个方法对于动态写入/读取配置文件非常有效。不过对于web应用程序这个方法可能不适合,因为大家知道,修改web.config相当于重启了web应用程序。

发布了28 篇原创文章 · 获赞 15 · 访问量 11万+

Guess you like

Origin blog.csdn.net/z3h0a5n8g8x9i9a2o3/article/details/9995229