【问题标题】:CloudConfigurationManager vs WebConfigurationManager?CloudConfigurationManager 与 WebConfigurationManager?
【发布时间】:2015-04-15 16:22:45
【问题描述】:

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

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

就在几天前,我偶然发现了 CloudConfigurationManager,有了它我可以得到这样的属性:

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

虽然 CloudConfigurationManager 似乎更适合云使用,但我从未遇到过 WebConfigurationManager 的任何问题。

  • 我应该使用 CloudConfigurationManager 吗?
  • 两者有什么区别?
  • 在什么情况下,CloudConfigurationManager 的行为会不同于
    WebConfigurationManager?

【问题讨论】:

    标签: c# azure azure-web-app-service


    【解决方案1】:

    CloudConfigurationManager 让我们无论身处何种环境都可以读取配置文件。

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

    WebConfigurationManager.AppSettings["MySetting"]

    对于 ServiceConfiguration.cscfg 文件:

    RoleEnvironment.GetConfigurationSettingValue("MySetting")

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

    CloudConfigurationManager.GetSetting("MySetting")

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        WebConfigurationManagerCloudConfigurationManager 管理不同的配置文件。

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

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

        【讨论】:

        • 但是 CloudConfigurationManager 工作正常并且可以完成 WebConfigurationManager 的工作 - 我的问题中的代码有效。
        • 是的,根据msdn,它运行良好,这是一种“奖励”逻辑。因此,如果您打算仅使用 Azure,则可以将其用于 web.config 和 cscfg 设置,但如果您想从 Azure 切换,则需要更改它。
        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-18
          • 1970-01-01
          相关资源
          最近更新 更多