Windows Azure: Startup task or OnStart(), which to choose?

As you may know that both “Startup task” and OnStart() function, are executed before your Role, Run function is called. You might have a question in your mind, if there any advantage of using startup task over executing the installation code in OnStart()?

 

For starters:

  • Startup task is something you define in CSDEF (Service Definition) file, within your role and expect to launch separately from your Windows Azure Application
  • OnStart() function is part of your Windows Azure application Role code where you write code to run within your Role Application. This code will be part of your Main Role DLL and will launch in a specific host process depend on your role type as below:
    • For Web Role your onStart() function code will run with WaIISHost.exe process
    • For Worker Role your onStart() function code will run with WaWorkerHost.exe process
    • For HWC, your onStart() function code will run with WaWebHost.exe process

In general, there is no conceptual difference between OnStart and a Startup task, however there are several small implementation details, that would make you choose one or the other:

  • A Startup task executes as a separate process so it can run at a different privilege level than the main entrypoint.
  • OnStart code runs in the same appdomain so it is easier to share state between OnStart and the main Run method, but otherwise you should see no difference.
  • For code that just needs to install software, using the startup task is preferable, mainly because you can run the startup task at a higher privilege, but keep the entrypoint at a lower privilege.
  • The timeouts for the two are the same, so if you are not out of your startup task or OnStart() function the role execution will progress further.
  • When your role is recycled, both startup task and OnStart() function will be executed again.
  • Startup up task can be configured to run ahead of your role similar to OnStart()
  • You can also setup a task to run parallel to your role as background or foreground process.

 

Good reading: