Configuration Changes In Azure Cloud Services

Azure service configuration is used in azure web and worker roles to determine application configuration settings that may change on an environment basis.

Azure Cloud Configuration

These can be changed at any time through the azure portal, without the need for a new deployment. This can be extremely useful, but how does it work?

Retrieving Azure Settings

Configuration settings can be obtained by calling GetConfigurationSettingValue on the RoleEnvironment class. This gets the string value which can then be cast to the appropriate type.

public static T GetRoleEnvironmentSetting(string settingName, T defaultValue = default(T))
    var value = RoleEnvironment.GetConfigurationSettingValue(settingName);
    if (!string.IsNullOrEmpty(value))
        return (T)Convert.ChangeType(value, typeof(T));
    return defaultValue;

Changes To Configuration Settings

When you update configuration in the Azure portal, configuration changes are applied to all roles in the deployment. This only works however for any configuration settings that are accessed after the changes have been made. What happens however if you set up configuration settings when a website/worker starts up, potentially as a singleton, or injected as a dependency into a class?

In this case configuration changes wont be applied, as the original values will still be held by the application. In this scenario you have 3 options to pick up changes:

  • Restart the web/worker role manually
  • Hook into the RoleEnvironment.Changing event to tell Azure to recycle the application. The RoleEnvironment.Changing event provides an argument of RoleEnvironmentChangingEventArgs. Setting the Cancel property to true will take the application instance offline to apply the change
  • Hook into the RoleEnvironment.Changed event to update and refresh your application settings. These will then pick up the new settings.

Leave a Reply

Your email address will not be published. Required fields are marked *