WCF autostart on Azure WebRole

I have a WCF hosted on Azure (WebRole). This WCF performs many background tasks and responds to some petitions.

The problem is that if the WCF does not receive any application for a long time (10 hours or more), the application pool is processed to an azure instance and the WCF tasks stop. I did a little investigation, and I can turn on the AutoStart function regarding the machine.config file, but this is not an option with azure deployment.

Can I enable AutoStart in web.config or deploy configuration files?

+8
c # azure wcf
source share
2 answers

You can add code to WebRole.cs to change the application pool:

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["/"]; var mainApplicationPool = serverManager.ApplicationPools[mainApplication.ApplicationPoolName]; mainApplicationPool["autoStart"] = true; 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(); } } 

Note: To use ServerManager , you need to:

  • link C: \ Windows \ system32 \ inetsrv \ Microsoft.Web.Administration.dll (or accessible via NuGet)
  • add <Runtime executionContext="elevated" /> to the service definition in the WebRole element
+11
source share

Although the Sandrino solution can work ... here is a solution that does not require the web role to be run in high security mode, and also forces the application to start when webrole starts (before the first user visits the site), This solution will also work on Earlier versions of IIS / Windows Server that do not require IIS 8 Application Initialization.

Just add WebRole.cs with the following content:

 using System; using System.Net; using System.Net.Security; using System.Threading; using Microsoft.WindowsAzure.ServiceRuntime; namespace Website { public class WebRole : RoleEntryPoint { public override bool OnStart() { WarmUpWebsite("HttpIn"); return base.OnStart(); } public override void Run() { while (true) { WarmUpWebsite("HttpIn"); Thread.Sleep(TimeSpan.FromMinutes(1)); } } public void WarmUpWebsite(string endpointName) { // Disable check for valid certificate. On som sites live HTTP request are redirected to HTTPS endpoint. And when running on staging SSL the certificate is invalid. RemoteCertificateValidationCallback allowAllCertificatesCallback = (sender, certificate, chain, sslPolicyErrors) => true; ServicePointManager.ServerCertificateValidationCallback += allowAllCertificatesCallback; try { RoleInstanceEndpoint endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[endpointName]; string address = String.Format("{0}://{1}:{2}", endpoint.Protocol, endpoint.IPEndpoint.Address, endpoint.IPEndpoint.Port); //This will cause Application_Start in global.asax to run new WebClient().DownloadString(address); } catch (Exception) { // intentionally swallow all exceptions here. } ServicePointManager.ServerCertificateValidationCallback -= allowAllCertificatesCallback; } } } 

Credits go to: http://weblogs.thinktecture.com/cweyer/2011/01/poor-mans-approach-to-application-pool-warm-up-for-iis-in-a-windows-azure-web- role.html

While (true) can be replaced with the Sandrino approach or you can disable the application pool timeout: http://blog.smarx.com/posts/controlling-application-pool-idle-timeouts-in-windows-azure

+3
source share

All Articles