Azure Configuration Settings (cscfg) with a return to Settings.Setting file

The definition of the configuration parameter in Azure ServiceConfiguration ( .cscfg

) is compelling because I can change the value in the Azure portal.

However, as discussed here Microsoft.WindowsAzure.CloudConfigurationManager.GetSettings("Foo")

will fall back to looking up the value <appSettings>

in app.config.

Is it possible to return it to a file Settings.setting

?

I could create a method like this , but is there a better / built-in way ?

public T GetSetting<T>(string name, T defaultValue = null)
{
    return
        !RoleEnvironment.IsAvailable
        //we could be in a non azure emulated environment (ie unit test)
        ? defaultValue 
        : RoleEnvironemt.GetConfigurationSettingValue(name)  
          ??
          //in case no value is specified in .cscfg or <appSettings>
          defaultValue;
}

      

And then you need to call it like this:

var settings = GetSetting("Example", Properties.Settings.Default.Example);

      

But this is a pain I have to specify a string parameter "Example"

+3


source to share


1 answer


I ended up creating a new overload for the above method and was able to pull the parameter name from the expression:

var setting = __cloudSettingsProvider.GetSetting(
   () => Properties.Setting.Default.ExampleConfiguration);

      

So now I can pass the name and default value. The method will check for Azure config

, then appSettings

, then applicationSettings

, and then finally hardcoded default in Settings.Settings

.



Here's the method code (essentially):

public T GetSetting<T>(Expression<Func<T>> setting)
{
     var memberExpression = (MemberExpression) setting.Body;

     var settingName = memberExpression.Member.Name;

     if (string.IsNullOrEmpty(settingName))
         throw new Exception(
            "Failed to get Setting Name " + 
            "(ie Property Name) from Expression");

     var settingDefaultValue = setting.Compile().Invoke();

     //Use the method posted in the answer to try and retrieve the
     //setting from Azure / appSettings first, and fallback to 
     //defaultValue if no override was found
     return GetSetting(
            settingName,
            settingDefaultValue);
}

      

+1


source







All Articles