Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the webrole onStart() event and Application_Start() global.asax event?

I'm just starting to get my feet wet learning the technical details of Azure, so apologies if this is a silly question.

If I create a cloud service project in visual studio and add a webrole for an mvc application, within the mvc application I can see the "WebRole.cs" file. However, when I start with an MVC application as the starting point, and later want to enable it for Azure, by right clicking on the project and selecting "Add Windows Azure Cloud Service Project", no WebRole.cs is created.

So where would I go to make things happen on the start event of the WebRole? The Application_Start() event of the Global.asax file?

If so, what's the difference between Application_Start() in Global.asax and the onStart() method of a webrole?

I've found the following post, which offers a partial explanation: What starts first Application_Start or WebRole's OnStart?

So if it's a case that the onStart event of the WebRole occurs before the Application_Start() in Global.asax, what happens if I want to run some code on the onStart() event in a project where I've later enabled the app for Azure?

like image 724
Steviebob Avatar asked May 21 '13 21:05

Steviebob


1 Answers

When there's no class extending RoleEntryPoint the web role will run just fine, just no extra code is run instead of OnStart(), Run() and OnStop().

Application_Start() is completely unrelated to Azure and is completely ignored by Azure runtime, it's just some piece of ASP.NET wiring. You can easily have Application_Start() unconditionally throwing an exception and that won't prevent your web role from getting started, just all HTTP requests will fail.

Bear in mind that starting with SDK 1.3 the default mode is "IIS mode" where the web role payload containing RoleEntryPoint descendant runs in one process (WaIISHost.exe) and ASP.NET code runs in another process. The process with RoleEntryPoint is started by Azure runtime first, it runs OnStart() and enters the infinite loop in Run(), then the instance is opened for HTTP requests. If you use IIS 7.5 and have "autostart" enabled you may have Application_Start() executed earlier, but otherwise you won't have Application_Start() executed until the first request comes.

So the key is that there're two different processes running your code and each has its own lifetime and that dictates limitations on how you can design your application.

The RoleEntryPoint descendant class can have any name, belong to any namespace and be located in any .cs file within the project which is selected as the payload for the web role - that will likely be your ASP.NET project. Under these conditions the RoleEntryPoint descendant will be located by Azure runtime and its methods will be run as part of role instance lifetime.

like image 79
sharptooth Avatar answered Oct 25 '22 13:10

sharptooth