Azure 配置设置 (cscfg) 回退到 Settings.Setting 文件
Azure Configuration Settings (cscfg) with fallback to a Settings.Setting file
在 Azure ServiceConfiguration (.cscfg
) 中定义配置设置很有吸引力,因为我随后可以在 Azure 门户中更改值。
但是,as discussed here Microsoft.WindowsAzure.CloudConfigurationManager.GetSettings("Foo")
将回退到在 app.config.
中寻找 <appSettings>
值
是否可以让它回退到 Settings.setting
文件?
我可以创建这样的方法,但是有 better/built-in 方法吗?
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;
}
然后必须这样称呼它:
var settings = GetSetting("Example", Properties.Settings.Default.Example);
但这很痛苦,我必须指定 "Example"
字符串参数
我最终为上述方法创建了一个新的重载,并且能够从表达式中提取设置名称:
var setting = __cloudSettingsProvider.GetSetting(
() => Properties.Setting.Default.ExampleConfiguration);
所以现在,我可以传入设置名称和默认值。该方法将检查 Azure config
,然后是 appSettings
,然后是 applicationSettings
,最后是 Settings.Settings
中的硬编码默认值。
这是该方法的代码(本质上):
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);
}
在 Azure ServiceConfiguration (.cscfg
) 中定义配置设置很有吸引力,因为我随后可以在 Azure 门户中更改值。
但是,as discussed here Microsoft.WindowsAzure.CloudConfigurationManager.GetSettings("Foo")
将回退到在 app.config.
<appSettings>
值
是否可以让它回退到 Settings.setting
文件?
我可以创建这样的方法,但是有 better/built-in 方法吗?
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;
}
然后必须这样称呼它:
var settings = GetSetting("Example", Properties.Settings.Default.Example);
但这很痛苦,我必须指定 "Example"
字符串参数
我最终为上述方法创建了一个新的重载,并且能够从表达式中提取设置名称:
var setting = __cloudSettingsProvider.GetSetting(
() => Properties.Setting.Default.ExampleConfiguration);
所以现在,我可以传入设置名称和默认值。该方法将检查 Azure config
,然后是 appSettings
,然后是 applicationSettings
,最后是 Settings.Settings
中的硬编码默认值。
这是该方法的代码(本质上):
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);
}