Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between OnStop() method and Stopping event in Windows Azure role?

Whenever a Windows Azure role is stopped its OnStop() method is invoked. Turns out that there's RoleEnvironment.Stopping event that is triggered before OnStop() is invoked. MSDN says this event is the right place for role clean shutdown code.

What's the difference between the two? Why would I put role clean shutdown code in Stopping event and not in OnStop() method override?

like image 746
sharptooth Avatar asked Nov 22 '11 13:11

sharptooth


3 Answers

Besides the fact that the event mechanism provides a flexible way to attach handlers, while the OnStop method has to be defined directly on the class derived from RoleEntryPoint, one relevant difference is this:

The Stopping event is not raised when the virtual machine of the role
instance is rebooted.

So the stopping event will not be raised, for instance, when the VM is rebooted for guest OS upgrade.

Another difference is this:

Code running in the OnStop method has 5 minutes to finish when it is called
for reasons other than a user-initiated shutdown.

While there is no mention in the documentation that the Stopping event has such a limit.

Source:

  • MSDN - RoleEnvironment.Stopping Event
  • MSDN - RoleEntryPoint.OnStop Method
like image 68
Fernando Correia Avatar answered Nov 14 '22 05:11

Fernando Correia


Events allow other subscribers in other classes to perform some action, whereas the method allows the subclass author such as yourself to place it in the actual class and (for example) modify which events get raised.

like image 32
Alex Norcliffe Avatar answered Nov 14 '22 05:11

Alex Norcliffe


Brent Stineman (Windows Azure MVP) recently blogged about the RoleEntryPoint and related start/run/stop sequence, and describes both Stopping and OnStop in the sequence descriptions.

like image 1
David Makogon Avatar answered Nov 14 '22 03:11

David Makogon