3

Defining a configuration setting in an Azure ServiceConfiguration (.cscfg) is compelling because I can then change the value within the Azure portal.

However, as discussed here Microsoft.WindowsAzure.CloudConfigurationManager.GetSettings("Foo") will fall back to looking for a <appSettings> value in the app.config.

Is it possible to have it fallback to a Settings.setting file instead?

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 have to call it like:

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

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

Community
  • 1
  • 1
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
  • It [falls back](https://msdn.microsoft.com/en-us/LIBRARY/microsoft.windowsazure.cloudconfigurationmanager) to web.config and app.config already. Are you trying to implement ALM scheme where you do not check-in your connection strings in cscfg? – Ognyan Dimitrov Jun 29 '15 at 14:58
  • 1
    @OgnyanDimitrov - Basically, I just didn't want to have 'magic' strings throughout my app to load configuration settings. I wanted the strongly typed and compile constants goodness from a settings file. – Philip Pittle Jun 30 '15 at 22:49
  • Definitely - no magic strings- is the way to go. I made two such templates - one for [console](https://github.com/ognyandim/StrongTypedConfig) and one for [azure](https://github.com/ognyandim/AzureStrongConfig) app using [Castle Dictionary Adapter](http://kozmic.net/2013/11/21/on-strongly-typed-application-settings-with-castle-dictionaryadapter/). – Ognyan Dimitrov Jul 01 '15 at 05:28

1 Answers1

1

I ended up creating a new overload for the method above and was able to pull out the name of the setting from an Expression:

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

So now, I can pass in the setting name and default value. The method will check against Azure config, then appSettings, then applicationSettings and then finally the hard coded default in Settings.Settings.

Here's the code for the method (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);
}
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123