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

kri*_*son 7 azure azure-web-roles

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.

谢谢

短剑的一种

San*_*tia 19

首先,您需要使用启动任务来安装该功能:

PKGMGR.EXE /iu:IIS-ApplicationInit
Run Code Online (Sandbox Code Playgroud)

然后,您需要在IIS中配置您的站点(startModepreloadEnabled):

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

我写了一篇关于此的博客文章,你可以在GitHub上找到一个示例应用程序.