0

I have this section in my web.config

<system.web>
    <caching>
      <sqlCacheDependency pollTime="60000" enabled="true">
        <databases>
          <add connectionStringName="CS" name="DB"/>
        </databases>
      </sqlCacheDependency>
    </caching>
</system.web>

I am trying to add a new entry in the <databases> element using this code

SqlCacheDependencySection section = ConfigurationManager.GetSection("system.web/caching/sqlCacheDependency") as SqlCacheDependencySection;
section.Databases.Add(new SqlCacheDependencyDatabase("DB2", "CS2", 60000)); 

But I receive an error that databases is read-only. The same goes for pollTime and enabled attributes.

So far I have managed to make them writable by setting the _bReadOnly private property to false using reflection

FieldInfo fi = typeof(ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(section, false);

I'm not very comfortable using reflection to change the value of a private property in a sealed .net class. Also, I don't want to edit the web.config file using ConfigurationManager.OpenExeConfiguration because this would restart the worker process.

Is there another way to configure SqlCacheDependency in code behind?

Ovidiu
  • 1,407
  • 12
  • 11

1 Answers1

-1

Not a direct answer, but maybe your problem can be solved by using config transformations at deploy time.

SergioC
  • 71
  • 5