Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically install Application Initialization in Azure Web Role (SDK v1.8, Windows Server 2012)

I see Microsoft have released Application Initialization as part of IIS 8.0. Unfortunately it isn't enabled in the Web Role by default. (by that I mean, "Application Initialization" as a feature of the web server role is not enabled. I know the Web Role has IIS 8.)

Does anyone know how I can enable this from a start-up script? I've already a number of start-up scripts, but I'm not sure how to add a server role feature.

The module itself appears inside Server Manager under "Server Roles" -> "Web Server (IIS)" -> "Web Server" -> "Application Development" -> "Application Initialization".

It's a shame that this isn't enabled by default as it will be very useful.

thanks

Kris

like image 346
krisdyson Avatar asked Nov 05 '12 17:11

krisdyson


People also ask

What is enable preload in IIS?

Setting preloadEnabled to "true" tells IIS 8.0 that it sends a "fake" request to the application when the associated application pool starts up. That is why in the previous step we set the application pool's startMode to "AlwaysRunning".

What is application initialization?

With IIS 8.0 Microsoft has introduced a new feature called Application Initialization. Application Initialization allows you to pre load some initialization tasks for your application before the application pool will handle user requests.


1 Answers

First you'll need to install the feature using a startup task:

PKGMGR.EXE /iu:IIS-ApplicationInit

And then you'll need to configure your site in IIS (startMode and preloadEnabled):

public class WebRole : RoleEntryPoint
{
    public override void Run()
    {
        using (var serverManager = new ServerManager())
        {
            var mainSite = serverManager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
            var mainApplication = mainSite.Applications["/"];
            mainApplication["preloadEnabled"] = true;

            var mainApplicationPool = serverManager.ApplicationPools[mainApplication.ApplicationPoolName];
            mainApplicationPool["startMode"] = "AlwaysRunning";

            serverManager.CommitChanges();
        }

        base.Run();
    }

    public override bool OnStart()
    {
        // For information on handling configuration changes
        // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

        return base.OnStart();
    }
}

I wrote a blog post about this and you can find a sample application on GitHub.

like image 57
Sandrino Di Mattia Avatar answered Nov 16 '22 04:11

Sandrino Di Mattia