Created
November 26, 2012 22:02
-
-
Save Dkowald/4150943 to your computer and use it in GitHub Desktop.
Azure role entry with config for hot-start of sites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Diagnostics; | |
| using System.Linq; | |
| using Microsoft.Web.Administration; | |
| using Microsoft.WindowsAzure.ServiceRuntime; | |
| namespace Kwd.Web | |
| { | |
| /// <summary> | |
| /// Application azure host. | |
| /// </summary> | |
| public class HostApplication : RoleEntryPoint | |
| { | |
| private readonly string[] _hotSites; | |
| public HostApplication() | |
| { | |
| //Site's from ServiceDefinition.def | |
| _hotSites = new string[] { "Web" }; | |
| } | |
| #region RoleEntryPoint | |
| public override bool OnStart() | |
| { | |
| bool res = base.OnStart(); | |
| if (res){ this.ConfigureHotStartSites(); } | |
| return res; | |
| } | |
| #endregion | |
| /// <summary> | |
| /// Config role(s) for hot-start. | |
| /// </summary> | |
| /// <remarks> | |
| /// Finding Mgmt Assem: http://blog.stronico.com/2010/01/how-to-fix-the-microsoft-web-administration-dll-problem/ | |
| /// Scrap doco for iis mgmt: http://www.iis.net/learn/manage/scripting/how-to-use-microsoftwebadministration | |
| /// Class: http://msdn.microsoft.com/en-us/library/ee758619.aspx | |
| /// Usage: http://brentdacodemonkey.wordpress.com/2011/09/24/leveraging-the-roleentrypoint-year-of-azure-week-12/ | |
| /// AppPool: http://blog.elastacloud.com/2011/01/26/windows-azure-apppool-settings-programmatic-modification/ | |
| /// Note: must have elevated excutionContext in web-role. | |
| /// </remarks> | |
| public void ConfigureHotStartSites() | |
| { | |
| Trace.TraceInformation("Configuring hot start sites."); | |
| foreach (var siteName in SiteNames()) | |
| { | |
| using (var serverManager = new ServerManager()) | |
| { | |
| Application application = null; | |
| ApplicationPool appPool = null; | |
| var site = serverManager.Sites.SingleOrDefault(s => s.Name == siteName); | |
| if (site != null){ | |
| application = site.Applications.FirstOrDefault(); | |
| } | |
| if (application != null) { | |
| appPool = serverManager.ApplicationPools.SingleOrDefault(p => p.Name == application.ApplicationPoolName); | |
| } | |
| if (appPool != null) { | |
| appPool["startMode"] = "AlwaysRunning"; | |
| appPool.ProcessModel.IdleTimeout = new TimeSpan(0); | |
| } | |
| if (application != null) { | |
| application["preloadEnabled"] = "true"; | |
| } | |
| if (appPool == null || application == null) { | |
| Trace.TraceError("Failed to configure site {0}", siteName); | |
| } else { | |
| Trace.TraceInformation("Site {0} first application configured for hot start", siteName); | |
| } | |
| serverManager.CommitChanges(); | |
| } | |
| } | |
| } | |
| public IEnumerable<string> SiteNames() | |
| { | |
| foreach (var item in _hotSites) { | |
| var siteName = String.Format("{0}_{1}" | |
| , RoleEnvironment.CurrentRoleInstance.Id | |
| ,item); | |
| yield return siteName; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment