title: Custom configuration section is null after app pool recycle
body: <p>Recently we have been encountering the following problem in production when reading our configuration file.</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object.
    (C:\applications\SampleWebsite\web.config line 105) at    at
    System.Configuration.BaseConfigurationRecord.EvaluateOne(String[]
    keys, SectionInput input, Boolean isTrusted, FactoryRecord
    factoryRecord, SectionRecord sectionRecord, Object parentResult)    at
    System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord
    factoryRecord, SectionRecord sectionRecord, Object parentResult,
    Boolean getLkg, Boolean getRuntimeObject, Object&amp; result, Object&amp;
    resultRuntimeObject)    at
    System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String
    configKey, Boolean getLkg, Boolean checkPermission, Boolean
    getRuntimeObject, Boolean requestIsHere, Object&amp; result, Object&amp;
    resultRuntimeObject)    at
    System.Configuration.BaseConfigurationRecord.GetSection(String
    configKey)    at
    System.Configuration.ConfigurationManager.GetSection(String
    sectionName)    at
    SampleWebsite.Configuration.CustomConfigurationSection.get_Current()
    in
    c:\Builds\1\SampleWebsite\Sources\Source\SampleWebsite\Configuration\CustomConfigurationSection.cs:line
    69    at
    SampleWebsite.Security.AuthorizationManager.CheckAccess(AuthorizationContext
    context) in
    c:\Builds\1\SampleWebsite\Sources\Source\SampleWebsite\Security\AuthorizationManager.cs:line
    161    at
    System.IdentityModel.Services.ClaimsPrincipalPermission.Demand()    at
    System.Security.PermissionSet.DemandNonCAS()    at
    SampleWebsite.CustomController.Test() in
    c:\Builds\1\SampleWebsite\Sources\Source\SampleWebsite\Controllers\CustomController.cs:line
    125    at lambda_method(Closure , Object , Object[] )    at
    System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.&lt;>c_<em>DisplayClass13.b</em>_c(Object
    instance, Object[] methodParameters)    at
    System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object
    instance, Object[] arguments)    at
    System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1
    func, CancellationToken cancellationToken)</p>
  </blockquote>
  
  <p>We are using .NET 4.5 on IIS 8 on a Windows Server 2012. The issue sometimes (not always!) occurs after the recycle of the application pool. If the issue occurs, we have to stop our application pool and start our application pool. Recycling the application pool does not help, neither does restarting the website.</p>
  
  <p>Nothing was changed in the configuration file. The only real difference we have noticed is that there are more requests made to the website itself.</p>
  
  <p>Since we first thought it could have been a race condition, we have scheduled the application pool to recycle at a specific time when we are sure that no requests are being send to the website.</p>
  
  <p>The recycle time has been chosen at a time when the applications that send requests to this site are shut down and before they are started up again. </p>
  
  <p>The application pool has the following configuration options set:</p>
  
  <ul>
  <li>Start Automatically: true</li>
  <li>Start Mode: AlwaysRunning</li>
  <li>Start application pool immediately: true</li>
  </ul>
  
  <p>The website has the following options set:</p>
  
  <ul>
  <li>Preload Enabled: true</li>
  <li>Start automatically: true</li>
  </ul>
  
  <p>Has anyone encountered this issue before?</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>EDIT:</p>
  
  <p>This is basically what's in the custom config section code:</p>
  
  <pre><code>public sealed class CustomConfigurationSection : ConfigurationSection
  {
      private static CustomConfigurationSection _current;
  
      public static CustomConfigurationSection Current
      {
          get
          {
              return _current
                  ?? (_current = ConfigurationManager.GetSection(CustomSectionName) as CustomConfigurationSection);
          }
      }
  }
  </code></pre>
  
  <p>The nullref occurs in the ConfigurationManager.GetSection() after the app pool is recycled and the _current field (a static) is nullified and needs to be read again from the config file.</p>