---
title: "正确处理 Azure OnStop 事件"
author: "bk_lin"
date: "2013-01-18T10:57:00Z"
tags: []
slug: "azure-onstop"
draft: false
lastmod: "2013-02-02T22:29:29Z"
id: 1785653
url: "https://bk-lin.iteye.com/blog/1785653"
---

**作者信息：**本篇文章是由Windows Azure 和 ASP.NET MVC组程序员/作家[**RickAnderson**](https://twitter.com/RickAndMSFT)所著。

**重启Web Roles**

如何重启是Windows Azure 中一个经常被忽视的问题。正确处理重启操作至关重要，这样您就不至于丢失数据或损坏您固有的数据，便于您快速关机、 重启和高效率地处理新的请求。Windows Azure Cloud Service每月大约重启两次用于更新操作系统。 (关于操作系统更新的详细，请参阅[RoleInstance Restarts Due to OS Upgrades](http://blogs.msdn.com/b/kwill/archive/2012/09/19/role-instance-restarts-due-to-os-upgrades.aspx)。）当一个页面应用程序关闭时，将触发[RoleEnvironment.Stopping](http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleenvironment.stopping.aspx)事件。因为由 Visual Studio 创建的 web role boilerplate不会去重写[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法，所以应用程序在关机前只有几秒钟时间来处理 HTTP 请求。如果您的 web role正忙着处理挂起的请求，有些请求就有可能会丢失。你可以延迟重启时间或让您的 web role推迟最多 5 分钟的时间来重写[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法和调用Sleep，但这并不是很理想。一旦引发[Stopping](http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleenvironment.stopping.aspx)事件，Load Balance(LB)就停止向web role发送请求，所以拖延关机时间长于处理挂起请求让您的虚拟机处于Sleep状态的时间，并不起什么作用。最佳的方法是等待[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法直到没有更多的请求，然后启动关机。关的越早，VM 重启并开始处理请求就越早。要想使用最佳方法关机，请将下面的代码添加到[WebRole](http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleentrypoint.aspx)类中。

![](http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/4251.aa.PNG)

上面的代码检查 ASP.NET 请求的当前的counter。只要有请求，该[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法将调用Sleep延迟关机。一旦当前请求的counter降为零， [OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)返回并启动关机。万一 web 服务器太忙而不能在 5 分钟内处理完挂起的请求，应用程序也会强行关闭。请记住一旦触发[Stopping](http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleenvironment.stopping.aspx)事件，LB 会停止将请求发送到 web role，所以除非您拥有大量的web role （或太少的实例），否则永远不要超过几秒钟来完成当前请求。

上面的代码为Trace写入数据，但除非您执行一个复杂的[On-DemandTransfer](http://msdn.microsoft.com/en-us/library/windowsazure/gg433075.aspx)，不然 [OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法的trace数据将永远不会出现在**WADLogsTable**上。后面，我将介绍如何使用[DebugView](http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx)来查看这些trace事件。以及如何在web role 的[OnStart](http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleentrypoint.onstart.aspx)方法中获取tracing working。

**Worker Roles的最佳重启方法**

在一个worker role中处理[Stopping](http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleenvironment.stopping.aspx)事件需要不同的办法。通常worker role在[Run](http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleentrypoint.run.aspx)方法中处理队列消息。该方法包括两个全局变量 ；一个通知Run方法[Stopping](http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleenvironment.stopping.aspx)事件已触发，另一个通知[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法可以安全地启动关机。（[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)返回后启动关机）。下面的代码演示了这两种全局方法。

![](http://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/5102.aa1.PNG)

[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)调用时，全局变量**onStopCalled**设置为 true，若没有队列事件需要处理，在[Run](http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleentrypoint.run.aspx)方法中就可以根据**onStopCalled**的值在第一层循环时判断是否关机。

**查看 OnStop Trace Data**

如前面所提到的，除非您执行一个复杂的[On-DemandTransfer](http://msdn.microsoft.com/en-us/library/windowsazure/gg433075.aspx)，不然 [OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法的trace数据将永远不会出现在**WADLogsTable**上。我们将使用[Dbgview](http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx)来查看这些trace事件。在SolutionExplorer中，右键单击云项目并选择**Publish**.

![](http://blogs.msdn.com/resized-image.ashx/__size/250x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/1565.aa3.png)

下载您的发布配置文件。在**Publish Windows Azure Application**对话框中，选择**Debug**并勾选**Enable RemoteDesktop for all roles**。

![](http://blogs.msdn.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/0116.aa4.png)

编译器会将Trace调用从发布版本中移除，所以您需要设置buildconfiguration为**Debug**来查看Trace数据。一旦应用程序发布并运行，在 Visual Studio 中，选择**Server Explorer** (Ctl + Alt + S）。选择**Windows Azure Compute**，然后选择您的云部署。（此项目中叫做t6，它是一个production部署）。选择 web role instance，单击鼠标右键，选择**Connect usingRemote Desktop**.

![](http://blogs.msdn.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/3223.aa5.png)

Remote DesktopConnection (RDC)将使用您在发布向导中指定的帐户名称，并提示您输入密码。在任务栏的左侧，选择**Server Manager**图标。

![](http://blogs.msdn.com/resized-image.ashx/__size/250x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/8463.aa6.png)

在**Server Manager**左选项卡，选择**Local Server**，然后选择**IE EnhancedSecurity Configuration** (IE ESC)。在IE ESC 对话框中选择关闭radio按钮。

![](http://blogs.msdn.com/resized-image.ashx/__size/450x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/1447.aa7.png)

启动InternetExplorer、 下载和安装[DebugView](http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx)。启动[DebugView](http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx)，并在**Capture**菜单中，选择**Capture GlobalWin32。**

![](http://blogs.msdn.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/8688.aa8.png)

选择filter图标，，然后输入下面的排除筛选器：

![](http://blogs.msdn.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/4532.0.PNG)

![](http://blogs.msdn.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/0246.aa9.png)

对于此测试，我在**About**操作方法中添加了[RoleEnvironment.RequestRecycle](http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.serviceruntime.roleenvironment.requestrecycle.aspx)方法，正如其名字所示，启动关机/重启序列。或者，您可以重新发布应用程序，还将启动关机/重启序列。

![](http://blogs.msdn.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/8561.aa10.png)

按照同样的操作也可以查看worker role VM中的trace data。选择worker roleinstance，用鼠标右键单击并选择**Connect using Remote Desktop。**

![](http://blogs.msdn.com/resized-image.ashx/__size/400x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-13-25/4130.000.PNG)

如果您想看到一篇这样的博客，是关于如何让获取trace数据出现在**WADLogsTable上**而不是用[OnStop](http://technet.microsoft.com/en-us/subscriptions/microsoft.windowsazure.serviceruntime.roleentrypoint.onstop.aspx)方法，请联系我。本篇的大部分内容来自于我和Tom上周发布的[Azuremulti-tier tutorial](http://www.windowsazure.com/en-us/develop/net/tutorials/multi-tier-web-site/1-overview/)。您一定在此篇文章中也看到了一些其他好的方法。

— — Rick  
[@RickAndMSFT](https://twitter.com/RickAndMSFT)

Reference: [http://blogs.msdn.com/b/windowsazure/archive/2013/01/14/the-right-way-to-handle-azure-onstop-events.aspx](http://blogs.msdn.com/b/windowsazure/archive/2013/01/14/the-right-way-to-handle-azure-onstop-events.aspx)