Tag Archives: iis

Automating IIs always running to auto start your application

Rick Strahl has an excellent article on how to configure IIS always running. He explains all the manual steps you have to do in order to do that. I’ll show you how to automate those steps. Usefull for example for azure cloudservices

Install Application Initialization Windows Feature

With this powershell, you install the feature “Application Initialization”.

Install-WindowsFeature -Name Web-AppInit

Set StartMode AlwaysRunning and PreloadEnabled

With this c# code you can create a small .exe to configure application initialization
You need to reference Microsoft.Web.Administration.dll located in %windir%\Windows\System32\inetsrv
There’s a nuget package with version from IIS7 that microsoft never updated anymore. I recommend to use the version you have installed.

 using (var serverManager = new ServerManager())
 {
    var appPool = serverManager.ApplicationPools["YourAppPoolNameHere"];
    appPool.SetAttributeValue("startmode", "AlwaysRunning");
    appPool.SetAttributeValue("autostart", true);
    var site = serverManager.Sites["YourSiteNameHere"];
    // This code assumes that you have only one application
    site.Applications.First().SetAttributeValue("preloadEnabled", true);

    serverManager.CommitChanges();
  }

And that’s it.