Do I have to restart the service to change the configuration file (app. config) for Windows services to take effect?

Source: Internet
Author: User

This issue was encountered in the previous phase of the write Windows service. When I was writing to get the value of a configuration file, I usually write a static method like this to get:

   1://          <summary>
   2:///          Get the number of records per processing
   3://          </summary>
   4://          <returns></returns>
   5:          int getrecordcount ()
   6:          {
   7:              int recordCount = 10000;
   8:              try
   9:              {
  Ten:                  recordCount = math.abs (int. Parse (configurationmanager.appsettings["RecordCount"  ));
  One:                  if (recordcount==0)
  :                  {
  :                      recordCount = 10000;
  :                  }
  :              }
  16:              
  :              {
  :                  recordCount = 10000;
  :              }
  :              return recordCount;
  :          }

RecordCount represents the number of records that are read from the database at a time. After the service has written the post-installation test, this parameter has been changed several times, at first its default value is 10000, and later changed to 500,100, 50 and 10 respectively. But after the change, I had to restart the service to get the configuration file to work. After a few times this kind of toss, feel the experience is too bad. On the Internet a search, found this article: Do you has to restart a Windows service if you change the app. config? In the original question, a friend gave the answer, that is, through ConfigurationManager. refreshsection Refresh A configuration node, we do not need to restart the service when we get the configuration value:

        <summary>///        per processing of records///</summary>//                <returns></returns>        try {configurationmanager.refreshsection ("appSettings");  Refreshes the named section, which is re-read from disk The next time it is retrieved. RecordCount = Math.Abs (int. Parse (configurationmanager.appsettings[return recordCount;}       

Tested, that's true.

You might ask, why did you add that line of Configurationmanager.refreshsection (named node);

View MSDN, explaining this: "Refresh the named section so that it will be read back from disk The next time it is retrieved ." Here's a description of the function in VS:

                //Summary:        //     refreshes the named section so that it will be re-read from disk The next time it is retrieved. ////                parameter:        void Refreshsection (string sectionname);     

Originally, after changing the configuration file, the order in which the application read the configuration was not read from the physical file. Instead of reading from its cache (the Configurationmanager.refreshsection method invalidates the cache of the specified configuration section without affecting the other sections), the configuration file must be forced to be flushed to read the changed configuration section information.

Undoubtedly, compared with the normal read configuration file node method, the method of the read configuration should have a little impact on performance, as to how much loss, will not cause performance bottlenecks, you understand.

Finally, from the MSDN example we can also see that this method is very effective for dynamic write/read configuration files. This approach may not be appropriate for Web applications, however, as you know that modifying the Web. config is equivalent to restarting the website application.

Do I have to restart the service to change the configuration file (app. config) for Windows services to take effect?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.