When running application deployed on Azure, you may need to retrieve the a configuration value set for the current deployment in your code. This can be done by simply using the GetConfigurationSettingValue method available in the RoleEnvironment class provided by Azure.

It is necessary to check if the current execution environment is Azure and the Role is running or not. For this purpose we use the IsAvailable property. Also, the block of code should be wrapped inside a try-catch block since the IsAvailable property will throw an exception when running inside a non-Azure environment and the Azure dlls are not available.

using Cloud = Microsoft.WindowsAzure.ServiceRuntime;
public static string GetConfig(string key)
{
    try
    {
        if (!Cloud.RoleEnvironment.IsAvailable) return null;
        return Cloud.RoleEnvironment.GetConfigurationSettingValue(key);
    }
    catch { return null; }
}

If you want your application to run inside both Azure and a non azure environment, then you can return the ConfigurationManager.AppSettings value when the Azure environment in not available.

Related Links

Advertisements

Share your thoughts

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s