AutoStart a WCF on Azure WebRole

岁酱吖の 提交于 2020-01-10 01:49:23

问题


I have a WCF hosted on Azure (WebRole). That WCF does a lot of background tasks and replies to some petitions.

The problem is that if the WCF doesn't receive any petition for a long time (10 hours or more) the application pool is recycled on the azure instance and the WCF tasks stops. I did a little investigation an I can enable a AutoStart feature touching the machine.config, but this is not an option with a azure deploy.

Can I enable AutoStart within web.config or deploy config files?


回答1:


You can add some code in the WebRole.cs to modify 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 will need to:

  • reference C:\Windows\system32\inetsrv\Microsoft.Web.Administration.dll (or available through NuGet)
  • add <Runtime executionContext="elevated" /> in your Service Definition under the WebRole element



回答2:


While Sandrino's solution might work... here is a solution that does not require the web role to run in elevated security mode, and also will force the application to start when the webrole starts (before the first user visits the site). This solution will also work on older versions of IIS/Windows Server that does not require IIS 8's "Application Initialization" feature.

Just add a 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 goes 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

The while(true) could be replaced with Sandrino's approach or you could disable application pool idle timeout: http://blog.smarx.com/posts/controlling-application-pool-idle-timeouts-in-windows-azure



来源:https://stackoverflow.com/questions/13703908/autostart-a-wcf-on-azure-webrole

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!