Azure类库访问GetConfigurationSettingValue与web.config

时间:2012-02-07 13:50:17

标签: c# azure class-library

我在Azure辅助角色和ASP.NET网站之间共享了一个类库。库中的方法需要从配置中提取值以确定它是否应该发送电子邮件。

在ASP.NET站点中,设置位于web.config中:

<add key="SendEmails" value="true"/>

在Azure辅助角色中,它位于ServiceConfiguration.Cloud.cscfg:

<Setting name="SendEmails" value="true"/>

我要做的是让我的类库能够访问任一配置设置,具体取决于它运行的环境。

3 个答案:

答案 0 :(得分:11)

创建自己的类来检索配置值,其中包含以下项目:

if (RoleEnvironment.IsAvailable)
 return RoleEnvironment.GetConfigurationSettingValue("mySetting");
else
 return ConfigurationManager.AppSettings["mySetting"].ToString();

RoleEnvironment.IsAvailable将检测是否应检测您是否在Windows Azure结构中并返回true。它将要求您在ASP.NET项目中包含Microsoft.WindowsAzure.ServiceRuntime程序集/引用。

如果您想了解更多信息,我会a blog post on this topic

答案 1 :(得分:4)

现在内置于最新的Azure SDK。您可以使用 CloudConfigurationManager.GetSetting(“”) 方法,它确定您是否在云中运行并从一个或另一个读取。这是 RoleEnvironment azure类的替代品。

http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.cloudconfigurationmanager.aspx

如果您正在使用nuget,则nuget包名称为 Microsoft.WindowsAzure.ConfigurationManager

答案 2 :(得分:2)

查看RoleEnvironment.IsAvailable。

你可以编写一个快速的“帮助器”类,根据RoleEnvironment.IsAvailable是否返回true来分支逻辑。如果为true,例如,从web.config读取,如果为false,则从云配置中读取。