Setting ASP.NET application on IIS always running with PowerShell

Switch off AppPool timeout with PowerShell script

When creating new website with new application pool, bu default IIS sets your application pool in a way to save resources when inactive. This is because it does not want to run your website all the time if is there is no activity on it and keep using resources on the host for idle worker processes (w3wp.exe).

This is the reason you might experience slow response when opening your website URL from time to time especially if your website has low traffic.

Applications such are internal tools behind login might not have hight traffic and high number of requests, but they might take time to start up because they might be caching large lookups or external data sources. Shutting down the application pool may cause poor performances of such applications.

This is caused by two properies on the Application pool which are as mentioned above set for saving resources on the host by default:

 Iis App Pool

Note

Setting values for these two application pool properties is not possible from application web.config as they are part of <Windows>\System32\inetsrv\config\applicationHost.config

  • Start mode [startMode] Configures Application pool to run on OnDemand mode or AlwaysRunning mode.
    Default value is OnDeman. This means your application pool will not start right away after added to IIS of if IIS or whole host OS are restarted, your pool will not start along with them. It will start only when there is an actual request made to your website. Setting this option to AlwaysRunning will start your application pool whan added to IIS or along with IIS start.

  • Idle Time-out [idleTimeout] Amount of time (in minutes) a worker process will remain idle before it shuts down. A worker process is idle if it is not processing requests and no new requests are received.
    Default value is 20 (minutes). This setting will make sure the worker process of your application pool is shut down if it is idle for long time (defined property value). This can be caused by low traffic t your website. It will cause resource releasing occupied by your application by canceling your application pool worker process (w3wp.exe). Setting this value to 0 will switch off idle timeout worker process cancellation.

It is smart to change default values of these proprieties for best performance of your website. If you have automated deployment and auto-scaling on your side you might have to automate this as default values as described are more concerned of your host resources rather than response performance.

For this you might want to use PowerShell. The following script sets these two properties for best performances for specific website:

import-module WebAdministration

$websiteName = "MyWebsite"

$appPool = Get-Item IIS:\Sites\$websiteName | Select-Object applicationPool
$appPoolName = $appPool.applicationPool

Set-WebConfiguration -Filter '/system.applicationHost/serviceAutoStartProviders' -Value (@{name="ApplicationPreload";type="WebApplication1.ApplicationPreload, WebApplication1"})
Set-ItemProperty IIS:\Sites\$websiteName -Name applicationDefaults.serviceAutoStartEnabled -Value True
Set-ItemProperty IIS:\Sites\$websiteName -Name applicationDefaults.serviceAutoStartProvider -Value 'ApplicationPreload'
Set-ItemProperty IIS:\AppPools\$appPoolName -Name autoStart -Value True
Set-ItemProperty IIS:\AppPools\$appPoolName -Name startMode -Value 1 #1 = AlwaysRunning, 0 = OnDemand
Set-ItemProperty IIS:\AppPools\$appPoolName -Name processModel.idleTimeout -Value "00:00:00" #0 = No timeout
    

References

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform including ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

Umbraco CMS

read more

Comments for this article