10

I have a number of appSettings in web.config that are used frequently (i.e. on each post back). Does the ConfigurationManager hold these values in process or are there any performance gains to be had by copying these values into application state on ApplicationStart() and subsequently retrieving them from there?

M4N
  • 94,805
  • 45
  • 217
  • 260
Oundless
  • 5,425
  • 4
  • 31
  • 33

1 Answers1

11

AFAIK the configuration is read and parsed at startup and the data is held in memory thereafter. I don't believe any performance gains from caching the state in application variables will be significant - though you may get improved readability as you don't litter the code with ConfigurationManager.AppSettings[...].

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
  • Thanks for the info. I use my own common method for accessing state, so the "litter" would only be in one place anyway. :-) – Oundless Aug 20 '09 at 09:14
  • What about the claims made by this article: http://www.dotnetperls.com/appsettings-cache They claim it is 30 times faster to cache these settings. – James Reategui Feb 08 '12 at 19:16
  • @James Reategui: Don't know - I'm surprised, and I don't know exactly what they were measuring. The point is - are you likely to be reading these values in a tight loop? If so, you can just read from the setting into a local variable, and that will be faster than properties in a static class ... – Vinay Sajip Feb 08 '12 at 22:25
  • 3
    I was also pretty sure that the Appsettings are cached and this Microsoft link: http://msdn.microsoft.com/en-us/library/aa478432.aspx confirms this; "It is important to realize that the entire section is read, parsed, and cached the first time we retrieve a setting value. From that point forward, all requests for setting values come from an in-memory cache, so access is quite fast and doesn't incur any subsequent overhead for accessing the file or parsing the XML." – Oliver Gray Aug 14 '13 at 15:39