16

In an Azure Websites I was always using the following code to fetch some values from the config's app settings:

string property = WebConfigurationManager.AppSettings["property"];  

Just a couple of days ago I stublemd upon CloudConfigurationManager, and with it I can get the property like so:

string property = CloudConfigurationManager.GetSetting("property"); 

Although CloudConfigurationManager seems like it's better fitted to cloud use, I never had any issues with WebConfigurationManager.

  • Should I be using CloudConfigurationManager?
  • What are the differences between the two?
  • In what cases CloudConfigurationManager will behave diffrent from
    WebConfigurationManager?
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Yaron Levi
  • 12,535
  • 16
  • 69
  • 118

4 Answers4

19

CloudConfigurationManager enables us to read configuration file regardless of the environment we are in.

So instead of writing environment specific code statements e.g., for web.config file:

WebConfigurationManager.AppSettings["MySetting"]

For ServiceConfiguration.cscfg file:

RoleEnvironment.GetConfigurationSettingValue("MySetting")

We can write the below statement, which will read values from all the configuration files i.e., app.config, web.config and ServiceConfiguration.cscfg.

CloudConfigurationManager.GetSetting("MySetting")

Sajad Deyargaroo
  • 1,149
  • 1
  • 7
  • 20
2

CloudConfigurationManager requires Microsoft.WindowsAzure.Configuration assembly, part of Azure SDK or separate NuGet.

WebConfigurationManager requires System.Web.Configuration assembly, part of .NET Framework.

RaSi
  • 470
  • 5
  • 8
1

WebConfigurationManager and CloudConfigurationManager manage different configuration files.

WebConfigurationManager is for managing website's web.config file(s) and it's appsettings and connections strings

CloudConfigurationManager is for managing .cscfg files (for cloud services). His benefit is that you can manage configurations and connections from the azure portal directly.

Lanayx
  • 2,731
  • 1
  • 23
  • 35
  • But CloudConfigurationManager is working fine and dose the job of WebConfigurationManager - The code in my question works. – Yaron Levi Apr 15 '15 at 18:19
  • Yes, it is working as well according to [msdn](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.cloudconfigurationmanager.aspx), it is kinda "bonus" logic. So, if you plan to use Azure only, you can use it for both web.config and cscfg settings, but if you ever want to switch from Azure, you need to change it. – Lanayx Apr 15 '15 at 18:37
1

I think you're better off using WebConfigurationManager. With it, you have access to ConnectionStrings as well as AppSettings. Both sets of settings you can update via the Azure Portal. They can then further be used in other Azure facilities/services, such as when configuring website backup. Check this out for more information: https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/

Irwin
  • 12,551
  • 11
  • 67
  • 97