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?
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With