Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Application Start on Azure Web Role

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();
    }
}
like image 637
Jordi Avatar asked Jan 09 '13 15:01

Jordi


People also ask

How do you deploy a web application on Azure?

To deploy to any Azure App service (Web app for Windows, Linux, container, Function app or web jobs), use the Azure App Service Deploy task. This task is automatically added to the release pipeline when you select one of the prebuilt deployment templates for Azure App Service deployment.

What is the difference between web Role and Worker Role in Azure?

The only difference between the two is how your role is hosted on the VMs: Web role: Automatically deploys and hosts your app through IIS. Worker role: Does not use IIS, and runs your app standalone.

Which Azure products can you use to launch a Web application?

Net Core, Java, Docker, Node. js and more. Launch websites quickly, with broad CMS support from the Azure Marketplace.


3 Answers

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.

like image 150
sharptooth Avatar answered Oct 23 '22 01:10

sharptooth


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

like image 1
Davide Dolla Avatar answered Oct 23 '22 03:10

Davide Dolla


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

like image 1
Brian Reischl Avatar answered Oct 23 '22 02:10

Brian Reischl