Force Application Start on Azure Web Role
Asked Answered
S

3

6

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();
    }
}
Subsidy answered 9/1, 2013 at 15:23 Comment(0)
C
3

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.

Coranto answered 10/1, 2013 at 8:12 Comment(5)
that's what I am trying at the moment XDSubsidy
I use the same solution for 4 production environment for clients. It's a must with multiple nodes (instances), random requests would all of a sudden be really slow. Obviously because they were running App Start on these requests. Can supply code if you want it?Hyperventilation
The webrole instance performs requests to it's own ip, then, each instance is responsible for staying alive. If you put that on App_Start, you still need the first request.Subsidy
@Jordi: I'd say GetResponseStream() and ReadToEnd() are unnecessary.Coranto
@Jordi: Btw your solution will send requests for the whole instance lifetime. Why not send only one request in OnStart()?Coranto
C
1

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

Chestnut answered 9/1, 2013 at 16:46 Comment(2)
Note that code in WebRole.cs runs in a different process than IIS. So some kinds of initialization (pre-loading data in memory, opening connections, etc) can't be done there.Cardie
Thanks Brian, that was exact what happened to me :(Subsidy
C
1

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

Cardie answered 10/1, 2013 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.