Recipe14.15.Inspect and Change Your Web Application Configuration


Recipe 14.15. Inspect and Change Your Web Application Configuration

Problem

You want to be able to modify some settings in your web application configuration file from within a web page.

Solution

Use the System.Configuration.WebConfigurationManager and System.Configuration.Configuration classes to access elements of your web application's configuration settings.

First get a Configuration object for the configuration settings by calling the OpenWebConfiguration method on the WebConfigurationManager:

 System.Configuration.Configuration cfg =     WebConfigurationManager.OpenWebConfiguration(@"/CSCBWeb"); 

Now use the Configuration object to get a specific section of the settings. The following code retrieves the SqlCacheDependencySection of the configuration:

 // Get the sqlCacheDependencySection. SqlCacheDependencySection sqlCacheDep = (SqlCacheDependencySection)cfg.GetSection( "system.web/caching/sqlCacheDependency"); 

The SqlCacheDependencySection allows for creating a new SqlCacheDependencyDatabase and adding it to the configuration, then saving the new configuration:

 // Create a database entry for the sql cache. SqlCacheDependencyDatabase sqlCacheDb = new          SqlCacheDependencyDatabase("pubs","LocalPubs",9000000); // Add our database entry for the caching. sqlCacheDep.Databases.Add(sqlCacheDb); // Enable it. sqlCacheDep.Enabled = true; // Poll once a minute. sqlCacheDep.PollTime = 60000; // Save our new settings to the cfg file. cfg.Save(ConfigurationSaveMode.Modified); 

This creates the following section in the web.config file for the application:

 <sqlCacheDependency enabled="True" pollTime="60000">   <databases>     <add name="pubs" connectionStringName="LocalPubs" pollTime="9000000" />   </databases> </sqlCacheDependency> 

Now the application is configured to allow a SqlCacheDependency to be created.

Discussion

This may seem like a lot of work at first. It would be pretty easy to rip through the web.config file using an XmlTextReader/Writer combination or an XmlDocument. But that would get the settings in only that web.config file, not all of the other web.config files that merge with the application-level one to make up the true configuration. The WebConfigurationManager allows for accessing the current settings at runtime, not just the static ones on disk in the multiple files.

One of the results of changing the configuration of a web application programmatically is that it can result in the restart of the application domain for the application. This can cause performance issues on your server. The other major area to consider is security. If the page that executes this code is not secured properly, the application/server hosting the page could be open to attack.


When the configuration is modified during the processing of the web page, the changes are not immediately reflected in the current configuration, as the page needs to finish processing before the configuration can be updated. In the earlier case in which a SqlCacheDependency is configured, the attempt to immediately construct the SqlCacheDependency object will throw an exception stating that the application is not configured to do this. To get around this, you can do the configuration setting work, then redirect back to the same page with a parameter in the query string that bypasses this setup code and moves right into the code that uses the new configuration (the creation of the SqlCacheDependency in this case):

 // If the initial request. if (Request.QueryString.Count == 0) {     // Add the sqlCache database entry     // to web.config.     TestConfig();     // Now redirect to ourselves adding a query string.     // We do this so that the change we made to     // web.config gets picked up for the code in     // CreateSqlCacheDependency and SetupCacheDependencies.     // as it depends on that configuration being present.     // If you just create the entry and call the setup     // code in the same page instance, the internal     // configuration stuff doesn't refresh and you get     // an exception when the code can't find the sqlCache     // section it needs.     Response.Redirect(Request.RawUrl + "?run=1"); } else {     // Run 14.10.     CreateSqlCacheDependency();     // Run 14.11.     SetupCacheDependencies(); } 

See Also

See Recipes 14.10 and 14.11; see the "WebConfigurationManager Class" and "System.Configuration Namespace" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net