Windows Azure: Settings Are Settings Are Settings..? Maybe Not!

So I’m back from Oz and have been tinkering with a small worker role app that grabs my Twitter feed and aggregates how many posts my friends have been doing as a scoreboard (I’ll post that tomorrow).

One of the design decisions I’ve had to make has been where to store the configuration settings.

See, in Windows Azure, you can either:

What’s the diff?

Well, when you store your settings in your Service Configuration and Definition files, you are able to change those settings without having to redeploy your application. If you use the App.config file, any change you make won’t be picked up until you redeploy your app.

Allow me to demonstrate.

So, first you need to define your setting in your ServiceDefinition file:


And add the ConfigurationSetting section into it:

 <?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="SettingsExample" 
  <WebRole name="WebRole">
      <!-- Must use port 80 for http and port 443 
      for https when running in the cloud -->
      <InputEndpoint name="HttpIn" protocol="http" port="80" />
      <Setting name="DefaultWelcome"/>

Next, define some values in the ServiceConfiguration file:


And set the values:

 <?xml version="1.0"?>
<ServiceConfiguration serviceName="SettingsExample" 
  <Role name="WebRole">
    <Instances count="1"/>
      <Setting name="DefaultWelcome" value="Manishkta"/>

Next, you need to access your setting in your app, so you use the RoleManager.GetConfigurationSetting() to read it, like so:

 using Microsoft.ServiceHosting.ServiceRuntime;

namespace SettingsExample_WebRole
    public partial class _Default : System.Web.UI.Page
        protected void Button1_Click(object sender, EventArgs e)
            lblGreeting.Text = 
            RoleManager.GetConfigurationSetting("DefaultWelcome") + 
            " " + txtName.Text;

Then deploy your app:


Now, when I run my app, it pulls the setting from my Windows Azure app deployment:


Next, let’s go change the setting live without messing with our deployment, to start, click the configure button:


Then change and save the setting:


Windows Azure will reconfig your app:


And it will now use the new setting:


This saves you from having to completely package up and redeploy your app every time you want to change a setting (due to changes in business rules or between production and staging environments for example).

Also, if you want to know how to paste code into your posts, check out this Live Writer Add-in! It rocks!

Chau :)

Technorati Tags: Windows Azure,Settings