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?
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.
I may be misunderstanding the question here, so please let me know if so.
You can certainly start with a regular ASP.NET project and later convert it to run in a Windows Azure Web Role. The WebRole.cs isn't technically required - the role should start without it. But, if you want to inject some logic as part of the role instance's startup process, you can add a WebRole class, inheriting from RoleEntryPoint, and override the OnStart(). There you could do things like configure diagnostics, create Windows Azure storage tables or queues, etc.
Generally, I would tend to put configuration/bootstrap logic in the Web Role's OnStart() - more platform specific config/bootstrap.
You can have single entry point class to your role that is inherited from RoleEntryPoint class. Normally workerrole.cs or webrole.cs are inherited from this.
You can give any class name provided it is inherited from RoleEntryPoint.
OnStart event of this class is getting fired when your role is becoming is ready.
You can write your initialization logic here before your application starts.
It could be initialization of IoC containers, Windows azure diagnostic configuration or anything.
Application_Start event is fired when you hit your website first time. You role is already in ready state.