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

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 https://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: