Retrieve <caching> element from web.config

402 Views Asked by At

I've been trying to get the caching element from my web.config but have thus far failed.

When using this code:

Configuration conf  = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);

I am able to get at the web.configh file. When i use

conf.GetSection("system.web/membership");

I succefuly get the membership section.

When i use

conf.GetSection("system.web/caching");

I get null.

Any ideas ?

part of the web.config below:

    <system.web>
<caching>
  <sqlCacheDependency enabled="true" pollTime="1000">
    <databases>
      <clear />
      <add name="Tests" pollTime="1000" connectionStringName="TestsConnectionString"/>          
    </databases>        
  </sqlCacheDependency>      
</caching>
        <authentication mode="Forms">
        <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
    </authentication>
    <membership>
        <providers>
            <clear/>
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
        </providers>
    </membership>

....

1

There are 1 best solutions below

0
On

Have you casted the return type correctly as OutputCacheSection?

const string outputCacheKey = "system.web/caching/outputCache";
var outputCacheSection = ConfigurationManager.GetSection(outputCacheKey) as OutputCacheSection;

Now say you wanted to get an attribute named connectionString within the first provider node e.g.

<caching>
  <outputCache enableOutputCache="true" defaultProvider="MyRedisOutputCache">
    <providers>
      <add name="MyRedisOutputCache" connectionString="myapp.redis.cache.windows.net:6380,password=hellopassword,ssl=True,abortConnect=False" type="Microsoft.Web.Redis.RedisOutputCacheProvider,Microsoft.Web.RedisOutputCacheProvider" />
    </providers>
  </outputCache>
</caching>

you could do something like this

string defaultProviderName = outputCacheSection.DefaultProviderName;
ProviderSettings ps = outputCacheSection.Providers[defaultProviderName];
var cs = ps.Parameters["connectionString"];