This article is based on Azure in Action, published on October, 2010. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ Use promotional code ‘java40beat’ and get 40% discount on eBooks and pBooks ]
Handling Configuration at Runtime
Introduction
In this article, we will modify configuration settings at runtime and find out how to track changes when they occur. Let’s jump on the Azure portal and get started.
Modifying configuration settings in the Azure portal
You can modify the values that you set in the service configuration file dynamically at runtime by using the Azure portal. First, though, you have to redeploy your application.
After your application is redeployed and running in the fabric, you can easily modify any of your configuration settings. Select either the production or staging version of the role, and then click Configure. You’re redirected to the page shown in figure 1, in which you can modify the runtime configuration settings for the role that you selected.
Figure 1 Modifying configuration settings in the Azure portalThe Azure portal doesn’t have any fancy editors; all you see is a big text box with your service configuration file in it. Bet you’re glad you paid attention during XML school.
The Azure portal lets you directly edit the contents of the configuration settings or upload a new version of the service configuration file. After you apply your changes, they’re instantly replicated to all instances of your web role and, in some cases, you don’t have to restart any of the roles.
Figure 2 shows that you’ve modified the configuration setting mySetting from “Hello Birds Hello Trees” to “Hello Birds”.
Figure 2 Your web page showing a configuration setting that you modified via the Azure portalWow, modifying your configuration settings is easily achieved through the Azure portal. What’s even cooler is that your role can pick up and run with the changes without needing a restart. The only issue is that you might need to store a local copy of the previous setting whenever that setting changes. Let’s see how to do that.
Tracking service configuration changes
When you change a configuration setting, Windows Azure raises two events that your application can handle:
- RoleEnvironment.Changing—Fires before the changes to the setting are applied. You can still read the existing setting before the change is applied.
- RoleEnvironment.Changed—Fires after the changes to the setting are applied. You might use this event to set up a new shared resource after a cleanup of a shared resource.
To hook up and define one of these events, you can use the code shown in listing 1.
Listing 1 Tracking changes to the service configuration
public class WebRole : RoleEntryPoint { public override bool OnStart() { DiagnosticMonitor.Start("DiagnosticsConnectionString"); RoleEnvironment.Changing += RoleEnvironmentChanging; return base.OnStart(); } private void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e) { if (e.Changes.Any(change => change is RoleEnvironmentConfigurationSettingChange)) { Trace.WriteLine("Configuration Setting Changed"); // Set e.Cancel to true to restart this role instance e.Cancel = true; } } }
This rounds out our look at how to use the ConfigurationSettings functionality in Windows Azure.
Summary
We modified configuration settings at runtime and found out how to track changes when they occur. Using its ConfigurationSettings functionality, Windows Azure can read application settings from the web.config file.