title: IIS application pool ignores Shutdown Time Limit (seconds) with c# application
body: <h2>Shutdown Time Limit (seconds) - problem</h2>
  <p>I have an asp.net web form application that needs to save in-memory Db on disc before it ends(triggered by recycling at specific time). The time for saving the db is now growing and it sometimes takes more than 5 minutes.</p>
  <p>I have increased &quot;Shutdown Time Limit (seconds)&quot; time for my application pool to 10 minutes but it did not help. If IIS triggers recycling event or I do it manually it always finished after 5 minutes.</p>
  <h2>Proof from log file</h2>
  <pre><code>2016-04-20 19:03:44.1431 - INFO [9588]: Application_End
  2016-04-20 19:03:44.1431 - INFO [9588]: Delay saving to total 444 seconds
  2016-04-20 19:03:45.1462 - INFO [9588]: Delay 1 second passed
  .
  2016-04-20 19:08:45.1762 - INFO [9588]: Delay 301 second passed
  </code></pre>
  <p><strong>301 second is the last line from the log.</strong></p>
  <p>In order to eliminate any side effects I have created a new empty asp.net web app.( .NET 4.5)  I use only simple method that logs every second a new line and I am able to reproduce the same behavior.</p>
  <pre class="lang-c# prettyprint-override"><code>using System;
  using System.Configuration;
  using System.Linq;
  using System.Threading;
  using NLog;
  
  namespace DemoApplicationLog
  {
      internal static class Log
      {
          public static Logger Instance { get; private set; }
          static Log()
          {
              LogManager.ReconfigExistingLoggers();
  
              Instance = LogManager.GetCurrentClassLogger();
          }
      }
  
      public class Global : System.Web.HttpApplication
      {
          protected void Application_End(object sender, EventArgs e)
          {
              Log.Instance.Info(&quot;Application_End&quot;);
              DelaySavingBySeconds(ConfigurationManager.AppSettings[&quot;DelaySavingBySeconds&quot;]);
          }
  
          private static void DelaySavingBySeconds(string interval)
          {
              Log.Instance.Info($&quot;Delay saving to total {interval} seconds&quot;);
  
              var value = ConfigurationManager.AppSettings[&quot;DelaySavingBySeconds&quot;];
              if (string.IsNullOrEmpty(value))
              {
                  interval = &quot;0&quot;;
              }
  
              var intervalInSeconds = Convert.ToInt32(interval);
  
              foreach (var second in Enumerable.Range(1, intervalInSeconds))
              {
                  Thread.Sleep(1000);
                  Log.Instance.Info($&quot;Delay {second} second passed&quot;);
              }
  
              Log.Instance.Info(&quot;Delaying passed!&quot;);
          }
      }
  }
  </code></pre>
  <p>Tested on:
  win 7, IIS 7.5.7600.16385
  win 2012R2, IIS 8.5.9600.16384</p>
  <p>I have set up a new application pool with .NET CLR Version v4.0</p>
  <h2>Facts I can reproduce</h2>
  <ol>
  <li>&quot;Shutdown Time Limit&quot; is ignored, if the value is greater than 5 minutes (300 sec.)</li>
  <li>&quot;Shutdown Time Limit&quot; works fine, if the value is lesser than 5 minutes</li>
  <li>If I change anything in the Web.config file and IIS recycle(caused by the change), application is <em>NOT</em> terminated after 5 minutes.</li>
  </ol>
  <p>Is it IIS bug or what could be possibly wrong?</p>
  <p>Thanks Petr</p>
  <p><a href="https://github.com/vojtechovsky/DemoApplicationLogIssue" rel="nofollow noreferrer">The demo web app is available on github</a></p>