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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With