Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET

Web.Config is Cached

Rate me:
Please Sign up or sign in to vote.
4.84/5 (16 votes)
18 Jan 2011CPOL2 min read 27.2K   16   10
There was a question from a student over on the Asp.Net forums about improving site performance. The concern was that every time an app setting was read from the Web.Config file, the disk would be accessed. With many app settings and many users, it was believed performance would suffer.

There was a question from a student over on the Asp.Net forums about improving site performance. The concern was that every time an app setting was read from the Web.Config file, the disk would be accessed. With many app settings and many users, it was believed performance would suffer.

Their intent was to create a class to hold all the settings, instantiate it and fill it from the Web.Config file on startup. Then, all the settings would be in RAM. I knew this was not correct and didn't want to just say so without any corroboration, so I did some searching.

Surprisingly, this is a common misconception. I found other code postings that cached the app settings from Web.Config. Many people even thanked the posters for the code.

In a later post, the student said their text book recommended caching the Web.Config file.

OK, here's the deal. The Web.Config file is already cached. You do not need to re-cache it.

From this article http://msdn.microsoft.com/en-us/library/aa478432.aspx

It is important to realize that the entire <appSettings> 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.

The reason the misconception is prevalent may be because it's hard to search for Web.Config and cache without getting a lot of hits on how to setup caching in the Web.Config file.

So here's a string for search engines to index on: "Is the Web.Config file Cached?"

A follow up question was, are the connection strings cached?

Yes. http://msdn.microsoft.com/en-us/library/ms178683.aspx

At run time, ASP.NET uses the Web.Config files to hierarchically compute a unique collection of configuration settings for each incoming URL request. These settings are calculated only once and then cached on the server.

And, as everyone should know, if you modify the Web.Config file, the web application will restart.

I hope this helps people to NOT write code!  

Steve Wellens

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

Comments and Discussions

 
GeneralMy vote of 5 Pin
kiran dangar31-Oct-11 2:58
kiran dangar31-Oct-11 2:58 
GeneralMy vote of 5 Pin
Reiss15-Aug-11 21:32
professionalReiss15-Aug-11 21:32 
Good links to finally settle the matter.
GeneralMy vote of 4 Pin
Ramu Sangabathula Original15-Feb-11 13:03
Ramu Sangabathula Original15-Feb-11 13:03 
GeneralMy Vote of 5 Pin
NevilleSwales24-Jan-11 21:40
NevilleSwales24-Jan-11 21:40 
GeneralRe: My Vote of 5 Pin
Steve Wellens3-Feb-11 18:07
Steve Wellens3-Feb-11 18:07 
GeneralMy vote of 5 Pin
Bryian Tan21-Jan-11 19:25
professionalBryian Tan21-Jan-11 19:25 
Generalmedia Pin
johnnyjames78921-Jan-11 2:12
johnnyjames78921-Jan-11 2:12 
Generaldebate Pin
Osarahinton19-Jan-11 8:06
Osarahinton19-Jan-11 8:06 
Generalthanks for sharing - have 5 Pin
Pranay Rana18-Jan-11 23:51
professionalPranay Rana18-Jan-11 23:51 
GeneralMy vote of 5 Pin
saini arun18-Jan-11 20:20
saini arun18-Jan-11 20:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.