Reputation: 10012
To prevent AppPool recycling every 20 minutes, I'd like to remove IIS AppPool Idle Timeouts when my Azure Web Role starts. My website is a Web Application Project.
How do I do this?
Upvotes: 24
Views: 8664
Reputation: 8244
In addition to @Edward Brey answer, If you want to change other common settings in that startup script, here's how you do that
rem Preload
%windir%\system32\inetsrv\appcmd list app /xml | %windir%\system32\inetsrv\appcmd set site /in -applicationDefaults.preloadEnabled:True
rem Disable idle
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00
rem Auto start
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.autoStart:true
rem Always running
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.startMode:AlwaysRunning
rem Disable recycling
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.recycling.periodicRestart.time:00:00:00
To see a list of available options per section, do
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -?
Upvotes: 1
Reputation: 41698
Create a startup task to disable the idle timeout:
In the website project referenced by your web role project, add a file Startup.cmd
to the root folder.
In the properties for Startup.cmd
, set Copy to Output Directory to Copy if newer.
Add this line to Startup.cmd
:
if exist %windir%\system32\inetsrv\appcmd.exe %windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00
The if exist %windir%\system32\inetsrv\appcmd.exe
qualifier is optional. It lets you use the same code on the Azure Emulator Express, so you don't need IIS installed or need to run Visual Studio as Administrator.
Save the file as UTF-8 without signature. (File > Advanced Save Options in Visual Studio.)
In your web role project, in ServiceDefinition.csdef
, add this to the WebRole
:
<Startup>
<Task commandLine="Startup.cmd" executionContext="elevated" />
</Startup>
Upvotes: 32
Reputation: 41698
Don't bother. You should really have a monitoring solution for your web role anyway. And now that it's built into the Azure dashboard, it's easier to turn on monitoring than to get the idle timeout configuration right (especially if you want to maintain least privilege).
Upvotes: 2
Reputation: 4873
This is the approach I took:
using (ServerManager iisManager = new ServerManager())
{
Application app = iisManager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"].Applications[0];
TimeSpan ts = new TimeSpan(0, 00, 00);
iisManager.ApplicationPoolDefaults.ProcessModel.IdleTimeout = ts;
iisManager.CommitChanges();
}
Requires:
using Microsoft.Web.Administration;
using Microsoft.WindowsAzure.ServiceRuntime;
Upvotes: 0
Reputation: 26012
Another option is to configure IIS Idle Time-Out Action to 'Suspend'. You can do it as a part of your web role startup script.
Command that you need is on the box as part of IIS setup (note that this will work with Windows Server 2012 R2 and up, with your code targeting .NET 4.5.1 framework and higher).
%windir%\system32\inetsrv\appcmd set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeoutAction:Suspend
You'll have to update your Azure Cloud Service configuration file (.cscfg) to use OS Family 4, as outlined by scottgu in his blog post.
Since startup actions run when your instances are provisioned and before web application is deployed to IIS, by setting application pool defaults will defacto set your application apppool idel time out action to Suspend.
Upvotes: 4
Reputation: 10012
In the root of your Web Application Project, create a file named WebRole.cs
with the following code:
public class WebRole : RoleEntryPoint
{
public override void Run()
{
RemoveIISTimeouts();
base.Run();
}
private void RemoveIISTimeouts()
{
Process.Start(
String.Format(@"{0}\system32\inetsrv\appcmd", Environment.GetEnvironmentVariable("windir")),
"set config -section:applicationPools -applicationPoolDefaults.processModel.idleTimeout:00:00:00");
}
}
Upvotes: 2