Use Your Azure Settings!

A lot of times you are used to creating your connection strings and important app settings in your web.config file. However, what happens when you are creating a worker process that you don’t necessarily have a config file for? What if you wanted to update a value without have to repost the code to your application?

Luckily, Microsoft provides us with the ability to specify settings values for each of the cloud pieces of our application. So if we open up a simple cloud application with a worker process then we can begin to see how this works.

 

The cloud project will be the one we are working with. We simply right click on the role that we want to attach settings to and select Properties. In our example, this would be WorkerRole1. Opening up the Properties window you will see the following:

 

Here you can specify how many instance and what size you want them to be for this particular role. You would click the Settings tab from the menu on the left hand side to access the Settings area.

As you can see, we have the ability to add settings here either as String or Connection String types. You may think that the Connection String would be the type used for inputting connection strings for things like database connections. However, if you open the editor you will see that this is only used for Azure Storage connections.

So instead we would put in our connection strings as normal String types. Then all we have left is to be able to use it in code. For this you simply have to use the RoleEnvironment.GetConfigurationSettingValue and pass along the key to the setting value.

var sqlConnectionString = 

    RoleEnvironment.GetConfigurationSettingValue("SQLServerConnection");

 

We might even only want to access the Settings values if we are in a cloud environment(local or Azure based). Microsoft provides an IsAvailable property on RoleEnvironment exactly for that. So the code below would check to see if we are in a cloud environment and if not then revert back and use a connection string using ConfigurationManager.

var sqlConnectionString = 

                (RoleEnvironment.IsAvailable) ? RoleEnvironment.GetConfigurationSettingValue("SQLServerConnection") : 

                ConfigurationManager.ConnectionStrings["DBTelemetryDatabase"].ConnectionString;

 

Hopefully, this helps you get the most out of your cloud based projects!

 

Ninja

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!