2

I currently use RoleEnvironment.GetConfigurationSettingValue(propertyName) to get the value of a setting defined in my WebRole config file (csdef + cscfg). Ok, sounds right.

This works well if the setting exists but failed with an Exception if the setting is not defined in the csdef and the cscfg.

I'm migrating an existing app to Azure which has many configuration settings in web.config. In my code, to read a setting value, I d'like to test : if it exists in the webRole config (csdef + cscfg) I read it from here, otherwise I read it with ConfigurationManager from web.config. This would prevent to migrate all settings from my web.config and allow to custom one when the app is already deployed.

Is there a way to do this ?

I don't want to encapsulate the GetConfigurationSettingValue in a try/catch (and read from web.config if I enter the catch) because it's really an ugly way (and mostly it's not performance effective !).

Thanks !

4

1 回答 1

3

1.7 Azure SDK 的更新。CloudConfigurationManager 类已被引入。这允许单个 GetSetting 调用首先查看您的 cscfg,然后如果未找到密钥,则回退到 web.config。

http://msdn.microsoft.com/en-us/LIBRARY/jj157248

Pre 1.7 SDK 简单的答案是否定的。(据我所知)

更有趣的话题是将配置视为依赖项。我发现将配置设置视为依赖项是有益的,这样支持的实现可以随着时间的推移而改变。该实现可能是用于测试的假冒或更复杂的东西,例如从.config/ .cscfg 切换到多租户解决方案的数据库实现。

鉴于此配置包装器,您可以将 TryGetSetting 编写为内部方法,无论您的配置选项来源是什么。将此功能添加到 RoleEnvironment 成员时,您只需更改该内部实现。

于 2011-05-16T14:41:38.890 回答