Force Application Start on Azure Web Role

2020-02-11 04:10发布

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();
    }
}

3条回答
Anthone
2楼-- · 2020-02-11 04:55

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).

查看更多
萌系小妹纸
3楼-- · 2020-02-11 05:00

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

查看更多
戒情不戒烟
4楼-- · 2020-02-11 05:08

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.

查看更多
登录 后发表回答