每当
Windows Azure角色停止时,都会调用其OnStop()方法.事实证明,在调用OnStop()之前触发了
RoleEnvironment.Stopping
event. MSDN说这个事件是角色清理关闭代码的正确位置.
除了事件机制提供一种灵活的方式来附加处理程序这一事实,而OnStop方法必须直接在从RoleEntryPoint派生的类上定义,一个相关的区别是:
The Stopping event is not raised when the virtual machine of the role instance is rebooted.
因此,不会引发停止事件,例如,当VM重新引导以进行客户操作系统升级时.
另一个区别是:
Code running in the OnStop method has 5 minutes to finish when it is called for reasons other than a user-initiated shutdown.
虽然文档中没有提到Stopping事件有这样的限制.
资源:
> MSDN – RoleEnvironment.Stopping Event
> MSDN – RoleEntryPoint.OnStop Method