Disable IIS Idle Timeouts in Azure Web Role

只谈情不闲聊 提交于 2019-12-03 18:35:56

问题


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?


回答1:


Create a startup task to disable the idle timeout:

  1. In the website project referenced by your web role project, add a file Startup.cmd to the root folder.

  2. In the properties for Startup.cmd, set Copy to Output Directory to Copy if newer.

  3. 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.

  4. Save the file as UTF-8 without signature. (File > Advanced Save Options in Visual Studio.)

  5. In your web role project, in ServiceDefinition.csdef, add this to the WebRole:

    <Startup>
      <Task commandLine="Startup.cmd" executionContext="elevated" />
    </Startup>
    



回答2:


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.




回答3:


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");
    }
}



回答4:


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).




回答5:


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 -?



回答6:


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;


来源:https://stackoverflow.com/questions/18089487/disable-iis-idle-timeouts-in-azure-web-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!