Windows Azure Cloud Service (9) Configuration的变更和通知机制

简介:

Windows Azure Platform 系列文章目录

 

  Windows Azure项目包含两个重要的配置文件:一个是Service Model定义文件ServiceDefinition.csdef;另外一个是配置文件ServiceConfiguration.cscfg。CSDEF文件一经部署就无法修改,除非重新部署整个Hosted Service;而CSCFG文件可以在Role运行状态下修改,类似于Web.config文件的修改机制。

  Microsoft.WindowsAzure.ServiceRuntime命名空间下面的RoleEnvironment类不但能够获取到Windows Azure平台的信息,还允许应用程序绑定CSCFG文件的变更时间。RoleEnvironment通过Changing和Changed两个事件通知调用方CSCFG文件发生了变化。需要注意的是,这两个事件都是在CSCFG文件发生修改之后才会触发的。

  • Changing事件:在配置信息被应用到这个Role Instance之前发生。开发人员可以通过事件参数RoleEnvironmentChangingEventArgs中的Cancel属性确定是否需要重启这个Role。如果Cancel=True,那么Fabric Controller将会从OnStart方法开始重新启动整个Role,否则将不会重启Role,但是修改之后的新内容也可以被获取到。
  • Changed事件:在配置信息被应用到这个Role Instance之后触发。

我们修改WebRole.cs里面的内容,加入对CSCFG文件变更的时间函数:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Diagnostics;

namespace WebRole1
{
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

// 当用配置文件中ConfigurationSettings时必须调用CloudStorageAccount.SetConfigurationSettingPublisher
// 来说明当配置文件在发布后被更改时将采取何种操作
CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
{
configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
RoleEnvironment.Changed += (sender, arg) =>
{
if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()

.Any((change) => (change.ConfigurationSettingName == configName)))
{
if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
{
RoleEnvironment.RequestRecycle();
}
}
};
});
RoleEnvironment.Changing += RoleEnvironmentChanging;

return base.OnStart();
}

private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
{
// If a configuration setting is changing
if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange))
{
// Set e.Cancel to true to restart this role instance
e.Cancel = false;
}
}


}
}
复制代码

  在RoleEnvironment的Changing事件上加入了一段代码,当CSCFG发生变化的时候将e.Cancel设置为False,不需要重启Role。

 

 

  通过修改e.Cancel=false,我们就可以在浏览正在部署的Cloud Service的DNS了。

  否则默认情况下,我们需要等待整个Cloud Service全部部署完毕才可以浏览。

 

 

  我在CSCFG加入相应的内容,设置LabelValue为Hello World

 然后我们修改Default.aspx文件,加入Label控件

并且修改后台的代码

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace WebRole1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblValue.Text = RoleEnvironment.GetConfigurationSettingValue("LabelValue");
}
}
}
复制代码

按F5调试项目,结果如下

这样就可以让ASPX页面的Label显示我在CSCFG设置的字符串。

 

然后我们把这个站点发布到Windows Azure平台上(过程略)。

然后我们登录Windows Azure用户管理界面,选择这个站点,选择"配置"

在弹出窗口里,修改配置文件。把LabelValue的值改成"Hello Windows Azure"

 当然我们也可以修改其他的配置文件,比如Instance count="2",这里我就不再详述。

修改完毕后,Windows Azure站点会进行更新。

我们不用管他,直接打开之前发布好的站点,按F5刷新页面

其中红色区域的"Hello Windows Azure"就是之前我在配置部署窗口里修改的内容。

 

总结:

在传统的ASP.NET应用程序里,我们读取的配置文件一般都是放在Web.config,并且通过

System.Configuration.ConfigurationSettings.AppSettings["SettingName"]来读取配置文件。

在Windows Azure里,我们也可以将配置文件写入Web.config里。但是因为我们的Web App其实是Azure VM远程托管运行的,如果需要修改配置文件的话我们不得不远程登录桌面,然后再修改IIS下的Web.config文件。管理起来非常复杂。

所以在一般情况下,Windows Azure项目的配置文件是写到CSCFG文件里的。然后通过RoleEnvironment.GetConfigurationSettingValue("cscfgName")来读取。

这样我们就可以直接通过配置部署窗口进行修改,而不需要远程桌面修改站点的Web.config或者重新发布Azure站点了。

 

Update 2015-03-26

注意:ServiceConfiguration的配置信息,可以通过上面的方法来进行修改。

  但是如果遇到需要修改项目文件,比如css, JavaScript, JSP, ASPX等,不能通过该方法修改。

 

 


本文转自Lei Zhang的博客博客园博客,原文链接:http://www.cnblogs.com/threestone/archive/2012/03/06/2380967.html,如需转载请自行联系原作者
目录
相关文章
|
5月前
|
安全 Windows
【Azure Cloud Service】在Windows系统中抓取网络包 ( 不需要另外安全抓包工具)
通常,在生产环境中,为了保证系统环境的安全和纯粹,是不建议安装其它软件或排查工具(如果可以安装,也是需要走审批流程)。 本文将介绍一种,不用安装Wireshark / tcpdump 等工具,使用Windows系统自带的 netsh trace 命令来获取网络包的步骤
135 32
|
5月前
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
87 11
|
8月前
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
|
8月前
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
|
8月前
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
125 1
|
8月前
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
|
8月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
8月前
|
存储 Linux Windows
【应用服务 App Service】App Service For Windows 如何挂载Storage Account File Share 示例
【应用服务 App Service】App Service For Windows 如何挂载Storage Account File Share 示例
|
30天前
|
Unix 虚拟化 Windows
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)
89 4
Windows Server 2025 中文版、英文版下载 (2025 年 3 月更新)