Creating configuration reader for web and cloud environments

Currently it is not possible to make changes to web.config file that is hosted on Windows Azure. If you want to change web.config you have to deploy your application again. If you want to be able to modify configuration you must use web role settings. In this blog post I will show you how to write configuration wrapper class that detects runtime environment and reads settings based on this knowledge.

The following screenshot shows you web role configuration that is opened for modifications in Windows Azure site.

Windows Azure web role configuration

My solution is simple – I will create one interface and two configuration reading classes. Both classes – WebConfiguration and AzureConfiguration – implement IConfiguration interface.

using System.Web.Configuration; 
using
Microsoft.WindowsAzure.ServiceRuntime;
 

namespace
MyWebApp
{
   
public interface IConfiguration
    {
       
string GetSetting(string
name);
    }
 
   
public class AzureConfiguration : IConfiguration
    {
       
public string GetSetting(string
name)
        {
           
return
 
             
RoleEnvironment
.GetConfigurationSettingValue(name);
        }
    }

   
public class WebConfiguration : IConfiguration
    {
       
public string GetSetting(string
name)
        {
           
return WebConfigurationManager.AppSettings[name];
        }
    }
}

Now we need something that we can use to read configuration settings without worrying about what settings exactly are read. I write simple static class that detects configuration reader and reads configuration settings we ask.

using Microsoft.WindowsAzure.ServiceRuntime;
 

namespace
MyWebApp
{
   
public static class Conf
    {
       
private static IConfiguration
_conf;
 
       
public static string GetSetting(string
name)
        {
           
if (_conf == null
)
                _conf = GetConf();
 
           
return
_conf.GetSetting(name);
        }
 
       
private static IConfiguration
GetConf()
        {
           
// If we are running in cloud
            if (RoleEnvironment
.IsAvailable)
               
return new AzureConfiguration
();
 
           
return new WebConfiguration();
        }
    }
}

We can use Conf.GetSetting(name) to read configuration parameters from currently active environment. Note how we are using RoleEnvironment to detect if we are in cloud. If you need the value of some setting you can just write code like this.

var appMode = Conf.GetSetting("WLA_ApplicationMode");

That’s it for now. As you can see it was not hard to extend our web application so we can read settings based on current environment. Static class is not maybe the best choice but it works for me. Now we have short and simple syntax for asking values of settings and if something changes we can make those changes to configuration classes and therefore we don’t have to modify classes that use those settings.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    Leave a Reply

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