Will ASP.NET catch exception thrown in WebRole class?
I thought I read somewhere that the WebRole runs in a different process than IIS on Windows Azure, making it possible to combine both Web and Worker roles http://things.smarx.com/#Combine%20Web%20and%20Worker%20Roles
Assuming the following generic code:
public class WebRole : RoleEntryPoint
{
public override void Run()
{
... Exception gets thrown here.
}
}
Does this require a separate exception handling approach? Is the Run di开发者_Python百科fferent than OnStart, meaning certain services have been started? Any best practices?
The title of the question and the inline question are different - which one are you most concerned about?
The WebRole in 1.3+ SDK can run full IIS, which runs under a different process than the RoleEntryPoint. This means for exception handling purposes, the RoleEntryPoint and the IIS web app are totally isolated. You would need explicit error handling in each as one does not apply to the other.
The other question you asked has to do with the Run vs OnStart. The OnStart method is called before your instance is connected to the LoadBalancer. This is your chance to bootstrap the role with anything you need to do before active traffic hits it. You must return true and not throw an error in OnStart or you will never get an active instance. Some folks use the OnStart to programmatically create the IIS stuff they need (sites, apps, vdirs, etc.). The Run method is your entry point to the main worker logic. It is like static void Main() (but one you never exit from).
The reality is that the Web and Worker roles are pretty much identical with the sole exception that the Web role has some nice declarative syntax now to setup IIS for you. All other caveats of runnig in a worker role apply to the web role when using the RoleEntryPoint.
RoleEntyPoint gets intialized before ASP.Net runtime is intitialized. As far as i remember if Run method throws exception the role would recycle,and you can see that in your Azure management portal. See this for some hints.
精彩评论