OnWtop()方法与Windows Azure角色中的Stopping事件之间的区别是什么?

时间:2011-11-22 13:22:33

标签: windows azure cloud azure-configuration

每当Windows Azure角色停止时,都会调用其OnStop()方法。事实证明,在OnStop()被调用之前触发了RoleEnvironment.Stopping event。 MSDN说这个事件是角色清理关闭代码的正确位置。

两者有什么区别?为什么我会在Stopping事件中放置角色干净关闭代码而不是OnStop()方法覆盖?

3 个答案:

答案 0 :(得分:4)

除了事件机制提供了一种灵活的方式来附加处理程序这一事实,虽然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事件有这样的限制。

来源:

答案 1 :(得分:2)

事件允许其他类中的其他订阅者执行某些操作,而该方法允许像您这样的子类作者将其放在实际的类中,并(例如)修改引发哪些事件。

答案 2 :(得分:1)

Brent Stineman(Windows Azure MVP)最近blogged关于RoleEntryPoint和相关的开始/运行/停止序列,并在序列描述中描述了Stopping和OnStop。