Force Application Start on Azure Web Role

喜夏-厌秋 提交于 2019-11-28 00:46:40

问题


I have a web role on azure and I would like to force a Application_Start without waiting for the first request.

I managed to set the "Start Automatically" property to true on my site

AutoStart a WCF on Azure WebRole

But the Application_Start is not called until the first request comes.

I don't know exactly if I am missing something important here. The server is a W2008 R2 and the IIS version is 7.5

Thanks!

SOLUTION

I put the solution code here. I hope will help someone. I just added a WebRole.cs and just put that code to perform a ping every 30 seconds. Please netice I'm browsing Service.svc because this is my endpoint, your endpoint could be another one. Notice I'm asking for "Endpoint1". If you have more than one endpoints, you should review that line.

public class WebRole : RoleEntryPoint
{        
    public override void Run()
    {            
        var localuri = new Uri( string.Format( "http://{0}/Service.svc", RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint ) );

        while (true)
        {
            try
            {                    
                var request = (HttpWebRequest)WebRequest.Create(localuri);
                request.Method = "GET";
                var response = request.GetResponse();
            }
            catch { }
            System.Threading.Thread.Sleep(30000);
        }            
    }

    public override bool OnStart()
    {               
        return base.OnStart();
    }
}

回答1:


The IIS will only start when the first request arrives. The workaround is to send an HTTP request to the same VM from within OnStart or your RoleEntryPoint descendant - that's easy using WebRequest or equivalent class.




回答2:


Jordi, I've recently experienced the same issue.

Based on my test Application_Start() is called ONLY when the 1st request ISS for the WebApp. (if you try to start VS in Debug without it open any page (see options in proj/debug), you will see that Application_Start() won't be called also if you don't run the WebApp in Azure)

I suppose that you need doing somethings when the WebRole start, well put your code in the WebRole.cs ;) Here you can override OnStart() and OnStop() and put your code that wiil be execuded when the WebRole will start.

I've used this way to run a BakgroundWorker that do some scheduled tasks, independently from IIS.

I hope this help. Davide.

Note: 1 - if you dont'have a WebRole.cs create it in the root of project and write inside: public class WebRole : RoleEntryPoint { public override bool OnStart() { ...your code... return base.OnStart(); } }

2 - If you need to debug the code mind that you need to run VS in debug with the Azure project that refer to WebApp as a "Run Project", otherwise the WebRole will not be called




回答3:


You could try putting some code in your WebRole.cs to request some URLs from your website. I've tried that, and it seems to work somewhat. But it's a pain to debug, so I never got it really nailed down.

Another option would be to use IIS Application Initialization. You can't use it on IIS 7.5, but you can get IIS 8 if you upgrade your roles to Windows 2012 (set osFamily="3" in your .cscfg).



来源:https://stackoverflow.com/questions/14238569/force-application-start-on-azure-web-role

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