ConfigurationSettings.AppSettings["foo"]

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Could someone provide me with details or a link on how the line of code
executes underneath the hood? Assume it's executed in an ASP.NET
application.

string blah = ConfigurationSettings.AppSettings["foo"]

Once your application has called for the "foo" key once, how expensive is to
call the same line again? My gut tells me that the CLR likely stores these
values in memory in a string dictionary or similar. Looking up the value
requires the potentially sorted collection to "lookup" the key again and
retrieve the value hopefully leveraging a b-tree or similar in the sorted
collection.

Perhaps it works a different way? Just curious. Thanks.

Mark
 
Hi,



| Could someone provide me with details or a link on how the line of code
| executes underneath the hood? Assume it's executed in an ASP.NET
| application.
|
| string blah = ConfigurationSettings.AppSettings["foo"]
|
| Once your application has called for the "foo" key once, how expensive is
to
| call the same line again?

Not much, only a lookup in some struct in memory ( hashtable, etc). At some
moment the file needs to be loaded. IMO (but I have no fact to prove it) it
happens when the application loads.

Also remember that even as the file remains closed the engine keeps an eye
on it (probably using a FileSystemWatcher) to detect change, if it does it
reload the entire app
 
It loads the first time ConfigurationSettings.AppSettings is called,
not necessarily on application load.

Hard to test in an ASP.NET app but in a winform/console app it's easy
to verify because you can change the app.config file before calling
ConfigurationSettings.AppSettings and your changes are reflect (not
usually a good idea but we did this in one situation where we wanted a
console app to share a config file with another app sothe console app
copied the file on startup).

Sam
 
if ur accessing web.config, use WebConfigurationManager - its faster than the
basic ConfigurationManager.
 
WebConfigurationManager is new to .NET 2.0 and the original post asked
about ConfigurationSettings.AppSettings which is obsolete in .NET 2.0,
implying he's using .NET 1.1.

Sam
 
Hi,

|
| It loads the first time ConfigurationSettings.AppSettings is called,
| not necessarily on application load.
|
| Hard to test in an ASP.NET app but in a winform/console app it's easy
| to verify because you can change the app.config file before calling
| ConfigurationSettings.AppSettings and your changes are reflect (not
| usually a good idea but we did this in one situation where we wanted a
| console app to share a config file with another app sothe console app
| copied the file on startup).

I think that the treatmean is different between web and win apps, in win app
as you mentioned before you can change the config while the app is running
and before the app access it without any consequence. If you do so in a web
app the application will restart.
 
The treament related to changing the file is different, but the O.P.'s
question was about when the data is loaded and how it's cached which
is the same--on first access.

Sam
 
Hi,

|
| The treament related to changing the file is different, but the O.P.'s
| question was about when the data is loaded and how it's cached which
| is the same--on first access.

Do you have a link where this is described?

Honestly I would find dificult to understand why it's loaded at first use,
and being "watched" from the beginning. Unless of course that the first
thing that happen when an app is loaded depends of the config, then it will
load (at the start of the app). In this escenario the file is loaded at
first use which coincide with the load of the app. IMHO I think this is the
way it happens
 
Use Reflector and look at ConfigurationSettings.GetConfig. It's
initialized on first use.

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top