且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-09-18 12:37:49

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文件变更的时间函数:

Windows Azure Cloud Service (9) Configuration的变更和通知机制
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;
}
}


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

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

 

 

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

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

 

 

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

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

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

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

并且修改后台的代码

Windows Azure Cloud Service (9) Configuration的变更和通知机制
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");
}
}
}
Windows Azure Cloud Service (9) Configuration的变更和通知机制

按F5调试项目,结果如下

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

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

 

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

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

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

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

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

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

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

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

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

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

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

其中红色区域的"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,如需转载请自行联系原作者