how to fix apps that stop after some time on windows azure

Recently I uploaded one of my web apps to Windows Azure. After the publish everything seemed to work alright but the next day, when I tried to view the app, it wouldn't start. I got some weird page null error and nothing else.

step 0: what makes sense

My first guess was that I had done something wrong, that some sort of exception was being thrown that Azure wouldn't let me see but after reviewing the logs I realized that no exceptions whatsoever had occurred.

step 1: what doesn't make sense

Since the above scenario wasn't the case and since the only reasonable explanation I could think of was that the web app after it's first stop (like regular IIS web apps stop after some period of inactivity) wouldn't start again (for some peculiar reason).

step 2: the solution

The solution (that seems to work for me) is simple and took just 17 or so lines of code: I added a new Worker Role to the solution that every minute makes a HttpWebRequest to the applications index page making sure that the app never stops because of inactivity.

This is the code I used:

public override void Start( )
{
	RoleManager.WriteToLog( "Information", "Worker Process entry point called." );
    
	while ( true )
    {
	    RoleManager.WriteToLog( "Information", "Working" );
    
		try
	    {
    		HttpWebRequest rq = HttpWebRequest.Create("http://path/to/your/web/root" ) as HttpWebRequest;

			WebResponse rs = rq.GetResponse( );
	        rs.Close( );
    	}
	    catch ( Exception )
    	{
    		RoleManager.WriteToLog( "Information", "Could not reach http://path/to/your/web/root" );
	    }

		RoleManager.WriteToLog( "Information", "Sleeping" );

        Thread.Sleep( 60000 );
	}
}

If you are experiencing similar problems I hope this may be of use to you.


Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Something useful (hopefully)

micro-blogging

@twitter: azazeal

Search

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my my associates' or empoyers' view in anyway.