
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Inspect and Change Your Web Application Configuration
|
819
// 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 ...