CloudConfigurationManager与WebConfigurationManager?

时间:2015-04-15 16:22:46

标签: c# azure azure-web-sites

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

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

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

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

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

  • 我应该使用CloudConfigurationManager吗?
  • 两者之间有什么区别?
  • 在什么情况下,CloudConfigurationManager的行为与来自不同 WebConfigurationManager?

4 个答案:

答案 0 :(得分:16)

CloudConfigurationManager 使我们能够阅读配置文件,无论我们处于何种环境。

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

  

WebConfigurationManager.AppSettings [" MySetting"]

对于ServiceConfiguration.cscfg文件:

  

RoleEnvironment.GetConfigurationSettingValue(" MySetting&#34)

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

  

CloudConfigurationManager.GetSetting(" MySetting&#34)

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

WebConfigurationManager CloudConfigurationManager 管理不同的配置文件。

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

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

答案 3 :(得分:1)

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