简体   繁体   中英

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)
   {


    }
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM