Avkash Chauhan's Blog

Windows Azure, Windows 8, Cloud Computing, Big Data and Hadoop: All together at one place.. One problem, One solution at One time...

Reading configuration entries using System.Configuration.ConfigurationManager class in a Windows Azure Application

Reading configuration entries using System.Configuration.ConfigurationManager class in a Windows Azure Application

Rate This
  • Comments 1

With Windows Azure application you can use ServiceConfiguration.cscfg to include any item-value pair and then in your role code you can read these settings directly however if you would want to read configuration entries using System.Configuration.ConfigurationManager class in an Azure application then you would need to use little different method to get it done. The potential reasons for different method are as below:

 

With Web Role:

  • In a web role OnStart() function runs within WaIISHost.exe which does not have any association with web.config and the web.config you had is part of w3wp.exe process which is running separately.

 

With Worker Role:

  • In worker role OnStart() function runs WaWorkerHost.exe and I believe with worker role you would not have a web.config associated with it.

 

So whenever you would need to use configuration entries in your OnStart() function using System.Configuration.ConfigurationManager class you would need to create an app.config file in your Windows Azure project and included all of your configuration related data in it.

 

Step 1: Adding app.config to your Windows Azure application:

Step 2: Adding Configuraiton items into App.config

<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v2.0.50727"/>
</startup>
<appSettings>
<add key="ConfigurationItem" value="ThisIsCongiruationItem" />
</appSettings>
</configuration>

Step 3: Reading Configuration related data in your Web Role OnStart() function as below:

 public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
// For information on handling configuration changes
// see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
string myConfiguration = System.Configuration.ConfigurationManager.AppSettings.Get("ConfigurationItem");
return base.OnStart();
}
}

Testing the code:

While running the code in compute Emulator you can verify the value is read correctly as below:

If you don’t have app.config and reading configuration data using Configuration Manager class you will get results as below:



Leave a Comment
  • Please add 8 and 3 and type the answer here:
  • Post
  • Nice info. I thought only service configuration can be used for settings but now I can also use app.config, very good idea. Thanks for nice post.

Page 1 of 1 (1 items)