Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Once the application starts up, how are values read from the web.config?

I'm curious how the web.config is loaded into a application, is any reference to values in the web.config actually parsing the web.config file, or upon application start does it load the values into a singleton or something?

This came to my mind as I wanted to check for a value in the web.config on a per request basis in the global.asax.cs:

protected void Application_BeginRequest(object sender, EventArgs e)
{
   if( ConfigurationManager.AppSettings["abc"] != null)
   {


    }
}
like image 731
loyalflow Avatar asked Mar 08 '13 20:03

loyalflow


2 Answers

When an application first gets a request, the config file(s) get parsed and its settings loaded. These settings are then cached, so that any subsequent call doesn't require re-parsing the config file(s). That is why when a config file is changed, the application restarts and gets recompiled again.

http://msdn.microsoft.com/en-us/library/ms178685.aspx#calculating_configuration_settings_at_runtime

like image 160
System Down Avatar answered Sep 28 '22 00:09

System Down


The configuration gets deserialized during application startup into the corresponding ConfigurationSection types.

These are normally implemented with read only properties, so they can be indeed used in a singleton.

like image 33
Oded Avatar answered Sep 28 '22 02:09

Oded