Search code examples
asp.netconfigurationcachingweb-configsqlcachedependency

How do I check if SqlCacheDependency is enabled at runtime?


I am using SqlCacheDependency with polling in an ASP.NET project.

Sometimes, I need to disable SqlCacheDependency, which I do as follows:

<caching>
  <sqlCacheDependency enabled="false" pollTime="10000">
    <databases>
      <!-- ... -->
    </databases>
  </sqlCacheDependency>
</caching>

However, this causes an error in my code when I do a HttpRuntime.Cache.Insert() with the SQL dependency.

To enable SQL cache dependency, please set the 'enabled' attribute to "true" in the section in the configuration file.

Is there a way I can programmatically check if this setting is enabled?


Solution

  • The following code worked for me...

    //using System.Web.Configuration;
    Configuration webConfig = WebConfigurationManager.OpenWebConfiguration("/");
    string configPath = "system.web/caching/sqlCacheDependency";
    SqlCacheDependencySection section = (SqlCacheDependencySection)webConfig.GetSection(configPath);
    bool enabled = section.Enabled;