Prepare your App for Windows Azure! Create a custom Configuration Manager!

When you deploy an application to Windowes Azure, your application configuration cannot be changed cause it becomes part of the distributed package.

So what can you do if you want to set some dynamic settings (i.e Connection String settings)?

First of all you must use the ServiceDefinition.csdef file and add the dynamic settings there.

Next tell your app to read the connection string from that file instead of the config file. To accomplish that task create the following classes:

 1namespace Azure.Configuration
 2{
 3    public class ConfigurationManager
 4    {
 5        private static Settings _settings = new Settings();
 6        public static Settings Settings
 7        {
 8            get
 9            {
10                return _settings;
11            }
12        }
13
14        public static ConnectionStrings ConnectionStrings
15        {
16            get
17            {
18                return new ConnectionStrings();
19            }
20        }
21    }
22
23    public class Settings
24    {
25        public string this[string key]
26        {
27            get
28            {
29                if (RoleEnvironment.IsAvailable)
30                {
31                    return RoleEnvironment.GetConfigurationSettingValue(key);
32                }
33                else
34                {
35                    if (cfg.ConfigurationManager.AppSettings.AllKeys.Contains(key))
36                        return cfg.ConfigurationManager.AppSettings[key];
37                }
38                return string.Empty;
39            }
40        }
41    }
42
43    public class ConnectionStrings
44    {
45        public string this[string key]
46        {
47            get
48            {
49                if (RoleEnvironment.IsAvailable)
50                {
51                    return RoleEnvironment.GetConfigurationSettingValue(key);
52                }
53                else
54                {
55                    if (cfg.ConfigurationManager.ConnectionStrings[key] != null)
56                        return cfg.ConfigurationManager.ConnectionStrings[key].ConnectionString;
57                }
58                return string.Empty;
59            }
60        }
61    }
62}

Then you can just call ConfigurationManager.Settings[{key}] to get the vaue for a given key or ConfigurationManager.ConnectionStrings[{connection string name}] to get a connection string.

You’ll need to add a reference to Microsoft.WindowsAzure.ServiceRuntime which is included in the Windows Azure Tools for Microsoft Visual Studio

Hope it helps!


SQL Azure Requires Clustered Indexes in all tables
In-Person or online Networking?
comments powered by Disqus