Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure class library access GetConfigurationSettingValue vs. web.config

I have a class library shared between an Azure Worker Role and a ASP.NET Website. A method in the library needs to pull a value from the config to determine if it should send an email or not.

In the ASP.NET site, the setting is in web.config:

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

And in the Azure worker role it is in ServiceConfiguration.Cloud.cscfg:

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

What I am trying to do is have my class library be able to access either config setting, depending on what environment it is running under.

like image 664
CodeGrue Avatar asked Feb 07 '12 13:02

CodeGrue


3 Answers

Create your own class to retrieve configuration values and in it you'll have items like this:

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

RoleEnvironment.IsAvailable will detect is supposed to detect if you're in the Windows Azure fabric and return true. It will require that you include the Microsoft.WindowsAzure.ServiceRuntime assembly/referrence in your ASP.NET project.

I did a blog post on this topic if you want more information.

like image 199
BrentDaCodeMonkey Avatar answered Nov 17 '22 20:11

BrentDaCodeMonkey


This is now built-in to the latest Azure SDK. You can use the CloudConfigurationManager.GetSetting("") method, it determines if you are running in the cloud or not and reads from one or the other. This is the replacement for the RoleEnvironment azure class.

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

If you are utilizing nuget, the nuget package name is Microsoft.WindowsAzure.ConfigurationManager

like image 32
John C Avatar answered Nov 17 '22 20:11

John C


Check out RoleEnvironment.IsAvailable.

You could write a quick "helper" class that branches logic based on if RoleEnvironment.IsAvailable returns true or not. If true, for example, read from web.config and if false, read from the cloud configuration.

like image 24
mcollier Avatar answered Nov 17 '22 19:11

mcollier