CloudConfigurationManager与WebConfigurationManager?

在Azure网站中,我总是使用以下代码从配置的应用程序设置中获取一些值:

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

就在几天前,我在CloudConfigurationManager上遇到了麻烦,有了它,我可以得到这样的属性:

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

虽然CloudConfigurationManager似乎更适合云使用,但我从来没有遇到任何WebConfigurationManager问题。

  • 我应该使用CloudConfigurationManager吗?
  • 两者有什么不同?
  • 在什么情况下,CloudConfigurationManager将表现出不同的行为
    WebConfigurationManager?

无论我们所处的环境如何, CloudConfigurationManager使我们能够读取配置文件。

因此,不要编写特定于环境的代码语句,例如,对于web.config文件:

WebConfigurationManager.AppSettings [ “MySetting”]

对于ServiceConfiguration.cscfg文件:

RoleEnvironment.GetConfigurationSettingValue( “MySetting”)

我们可以编写下面的语句,它将读取所有配置文件中的值,即app.config,web.config和ServiceConfiguration.cscfg。

CloudConfigurationManager.GetSetting( “MySetting”)

CloudConfigurationManager需要Microsoft.WindowsAzure.Configuration程序集,Azure SDK的一部分或单独的NuGet。

WebConfigurationManager需要System.Web.Configuration程序集,它是.NET Framework的一部分。

WebConfigurationManagerCloudConfigurationManager管理不同的配置文件。

WebConfigurationManager用于管理网站的web.config文件及其appsettings和连接字符串

CloudConfigurationManager用于管理.cscfg文件(用于云服务)。 他的好处是您可以直接从azure门户管理配置和连接。

我认为你最好使用WebConfigurationManager。 有了它,您可以访问ConnectionStrings以及AppSettings。 您可以通过Azure门户更新这两组设置。 然后,它们可以进一步用于其他Azure工具/服务,例如配置网站备份时。 请查看此信息以获取更多信息: https : //azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/