1

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>

....

JanivZ
  • 2,265
  • 3
  • 25
  • 31
  • 1
    Take a look at this question and the first answer: http://stackoverflow.com/questions/3021877/how-to-read-system-web-section-from-web-config – Jon Grant Feb 28 '11 at 14:00
  • thanks ! thats exactly what i needed ! ( now if i could work out how to mark your comment as the answer ... ) – JanivZ Feb 28 '11 at 14:10

1 Answers1

1

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"];
Frank Fu
  • 3,483
  • 2
  • 28
  • 38