I have seen a number of issues where the Windows Azure role is stuck when you deploy it and you can’t get it started. There are a few great posts about this problem at:
- Windows Azure role stuck in Initializing/Busy/Stopping
- Another common reason for Windows Azure role to stuck in Initializing/Busy/Stopping
One of the problems outlined here shows up because an exception is thrown in the OnStart function inside the WebRole.cs file in your Windows Azure WebRole. If an exception happens here, it becomes an unhandled exception because there is no try/catch and so the process that it is running in will be shut down. There is a great description of this at CLR Inside Out- Unhandled Exception Processing In The CLR.
So one way to help this situation is to add a try/catch inside the OnStart function. Below is such an example but I took it a step further and added code in the catch that will log the exception in the Event Log. That way we can see what is happening and investigate the problem.
Note: If the exception is caused by the Diagnostics code, you can’t use that to get the event log data, but you would be able to remote into the machine and look at the event log on the machine. The steps for that are here.
Here is what that function would look like:
I added a region so you can minimize the catch code and just focus on your code inside the try. Hope this helps people to be able to handle problems in this function better and not have it keep the Role from starting up.
One last thing, I put this under the ASP.NET Source in the Event Log. So that is what you would look for in regards to entries. You can change this to something else, but keep in mind that if you add your own event source, the process can’t use it until it is restarted. There is an example doing that here. This is the same code that is shown in Unhandled exceptions cause ASP.NET-based applications to unexpectedly quit in the .NET Framework 2.0.