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.

Changing web role settings

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

C#
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];

        }

    }
}


VB.NET

Imports System.Web.Configuration

Imports Microsoft.WindowsAzure.ServiceRuntime

 

Namespace MyWebApp

 

    Public Interface IConfiguration

        Function GetSetting(ByVal name As String) As String

    End Interface

 

 

    Public Class AzureConfiguration

        Implements IConfiguration

 

        Public Function GetSetting(ByVal name As String) _
            As String Implements IConfiguration.GetSetting

 

            Return _
              RoleEnvironment.GetConfigurationSettingValue(name)

        End Function

 

    End Class

 

    Public Class WebConfiguration

        Implements IConfiguration

 

        Public Function GetSetting(ByVal name As String) _
            As String Implements IConfiguration.GetSetting

 

            Return WebConfigurationManager.AppSettings(name)

        End Function

 

    End Class

End Namespace


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.

C#

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();

        }

    }

}


VB.NET

Imports Microsoft.WindowsAzure.ServiceRuntime

 

Namespace MyWebApp

 

    Public Module Conf

        Private Sub New()

        End Sub

 

        Private _conf As IConfiguration

 

        Public Function GetSetting(ByVal name As String) As String

            If _conf Is Nothing Then

                _conf = GetConf()

            End If

 

            Return _conf.GetSetting(name)

        End Function

 

        Private Function GetConf() As IConfiguration

            ' If we are running in cloud

            If RoleEnvironment.IsAvailable Then

                Return New AzureConfiguration()

            End If

 

            Return New WebConfiguration()

        End Function

 

    End Module

End Namespace


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.

kick it on DotNetKicks.com pimp it 顶 Progg it Shout it
Shout it!

No Comments