ConfigurationManager.AppSettings缓存

Jad*_*ias 43 .net asp.net performance caching configurationmanager

我们知道IIS缓存ConfigurationManager.AppSettings,因此它只读取磁盘一次,直到更改了web.config.这样做是出于性能目的.

有人在:

http://forums.asp.net/p/1080926/1598469.aspx#1598469

表示.NET Framework不会对app.config执行相同操作,但它会从磁盘读取每个请求.但我觉得很难相信,因为它会变慢.请告诉我他错了或者我必须修复我写的每个控制台/ Windows窗体/ Windows服务.

更新我很遗憾我误解了人们在上面的链接论坛中所说的内容.

Zom*_*eep 49

快速测试似乎表明这些设置仅在应用程序启动时加载.

//edit the config file now.
Console.ReadLine();

Console.WriteLine(ConfigurationManager.AppSettings["ApplicationName"].ToString());
Console.WriteLine("Press enter to redisplay");

//edit the config file again now.
Console.ReadLine();
Console.WriteLine(ConfigurationManager.AppSettings["ApplicationName"].ToString());
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

您会看到所有输出保持不变.

  • 只是为了挑剔,设置会在第一次被引用时加载,而不一定是在应用程序启动时. (19认同)
  • 更挑剔(以及关闭主题) - 无需调用ToString() - 它已经是一个字符串 (8认同)
  • 您必须调用`ConfigurationManager.RefreshSection("appSettings")`才能获得更改.您还可以添加文件观察程序,仅在其更改时重新加载. (3认同)