When IIS restarts your web application approximately every 26 hours, you're likely experiencing the default application pool recycling settings. This behavior is by design in IIS, though it can certainly cause headaches with session state loss.
IIS application pools have several built-in recycling triggers:
Beyond the default 26-hour cycle, consider these potential causes:
- IIS Idle Timeout settings (default: 20 minutes of inactivity)
- Memory-based recycling thresholds
- Application pool configuration changes
- Worker process crashes (check Windows Event Logs)
Here's how to properly log recycling events in Global.asax:
protected void Application_Start()
{
// Configure email notification
string body = $"Application restarted at {DateTime.UtcNow} UTC";
SmtpClient client = new SmtpClient("mailserver");
client.Send("noreply@yourserver.com", "admin@yourdomain.com",
"IIS Application Restart", body);
// Alternative: Log to file
File.AppendAllText(Server.MapPath("~/App_Data/restarts.log"),
$"{DateTime.UtcNow:o} - Application Start\n");
}
To prevent unwanted recycling, adjust these settings in applicationHost.config:
For applications where recycling is unavoidable, implement resilient session handling:
// In Startup.cs or Global.asax
protected void Session_Start(object sender, EventArgs e)
{
// Reinitialize critical session variables
if (Session["CriticalData"] == null)
{
Session["CriticalData"] = LoadFromDatabase();
}
}
// Consider using distributed session state
services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = Configuration.GetConnectionString("SessionDb");
options.SchemaName = "dbo";
options.TableName = "Sessions";
});
Enable detailed recycling logging in PowerShell:
Import-Module WebAdministration
Set-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"
-filter "system.applicationHost/applicationPools/applicationPoolDefaults/recycling"
-name "logEventOnRecycle"
-value "Time,Memory,PrivateMemory,Requests,Schedule,ConfigChange,IsapiUnhealthy,OnDemand"
Many ASP.NET developers encounter sudden session state loss in production environments. In my case, the symptom manifested as users being randomly logged out of an MVC application. After confirming the issue wasn't related to session timeout settings, I added this diagnostic code to Global.asax:
protected void Application_Start()
{
// Send email notification on application start
SmtpClient client = new SmtpClient();
client.Send("monitor@domain.com", "admin@domain.com",
"Application Restart Detected",
DateTime.Now.ToString());
// Other startup code...
}
The regular 26-hour interval between restarts pointed to IIS's default application pool recycling settings. IIS automatically recycles application pools to:
- Prevent memory leaks
- Clear accumulated resources
- Apply configuration changes
Through further investigation, I found these potential causes:
<system.webServer>
<applicationPools>
<add name="MyAppPool"
autoStart="true"
startMode="AlwaysRunning">
<recycling logEventOnRecycle="Time, Requests, Schedule">
<periodicRestart time="26:00:00">
<schedule>
<add value="03:00:00" />
</schedule>
</periodicRestart>
</recycling>
</add>
</applicationPools>
</system.webServer>
For mission-critical applications requiring persistent state:
// Option 1: Use out-of-process session state
<sessionState mode="SQLServer"
sqlConnectionString="Data Source=server;Integrated Security=SSPI;"
timeout="60" />
// Option 2: Implement distributed caching
services.AddDistributedMemoryCache();
services.AddSession(options => {
options.IdleTimeout = TimeSpan.FromHours(2);
options.Cookie.HttpOnly = true;
});
For better control over recycling behavior:
- Set specific recycling times during low-traffic periods
- Configure overlapping recycling to prevent downtime
- Disable recycling for AlwaysRunning applications
appCmd set apppool "MyAppPool" /recycling.periodicRestart.time:00:00:00
appCmd set apppool "MyAppPool" /processModel.idleTimeout:00:00:00