Original address: http://www.cnblogs.com/jeffwongishandsome/archive/2011/04/24/2026381.html
Workaround: Refresh the file before reading the configuration fileRefreshes the named section, which is re-read from disk The next time it is retrieved. Configurationmanager.refreshsection ("appSettings"); recordCount = Math.Abs (int. Parse (configurationmanager.appsettings["RecordCount"));
analysis:. NET read configuration file is generally read from the cache, after modifying the configuration file does not affect the value in the cache, and the read before the refresh file. NET reads the profile value from the hard disk file rather than from the cache
Performance Comparison:Preconditions : Six configuration items in app. Config
read 10,000 times after calling Refreshsection: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}: minute {2:00}: seconds: {3:00}: milliseconds",ts. Hours, TS. Minutes, TS. Seconds,TS.MILLISECONDS/10);//00: Time 00: minutes 07: seconds: 43: Millisecondsstring KK = ElapsedTime;read 10,000 times after not calling Refreshsection:
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}: minute {2:00}: seconds: {3:00}: milliseconds",ts. Hours, TS. Minutes, TS. Seconds,TS.MILLISECONDS/10);//00: Time 00: minutes 00: seconds: 01: Millisecondsstring KK = ElapsedTime;
Conclusion:Using Refreshsection to refresh the file before reading has a significant impact on performance
Troubleshoot issues that must be restarted after the Windows service modifies a configuration file