【问题标题】:Using ConfigurationManager in web apps在 Web 应用程序中使用 ConfigurationManager
【发布时间】:2013-05-19 02:39:05
【问题描述】:

据我了解,我应该使用 System.Configuration.ConfigurationManager 来读取桌面应用程序中的应用程序设置,并使用 System.Web.Configuration.WebConfigurationManager 在 Web 应用程序中执行相同的操作。这是 MSDN 所说的。

我正在开发一个将部署到桌面和 Web 服务器的组件,因此我试图使配置逻辑独立于平台。令我惊讶的是,我偶然发现在 Web 服务器上使用 System.Configuration.ConfigurationManager 可以正确读取应用设置。

我有两个相关的问题:

  1. 谁能确认这系统有效?
  2. 在 Web 应用程序上使用 System.Configuration.ConfigurationManager 有哪些限制或风险?

谢谢。

【问题讨论】:

    标签: .net web-config app-config configurationmanager


    【解决方案1】:

    MSDN saysWebConfigurationManager 是在 Web 应用程序中使用配置文件的首选方式。

    在 Web 应用程序中,可以从父虚拟目录中的 web.config 文件继承配置:WebConfigurationManager 将正确处理此类继承的设置,我认为 ConfigurationManager 不是这种情况。

    当然,如果您的应用程序不继承此类设置(例如,始终位于网站的根目录),这与您无关。

    【讨论】:

      【解决方案2】:

      System.Web.Configuration.WebConfigurationManager.AppSettingsSystem.Web.Configuration.WebConfigurationManager.ConnectionStrings 在内部使用相同的 System.Configuration.ConfigurationManager - 所以没有区别。

      MSDN 说:

      这些方法执行只读操作,使用配置的单个缓存实例,并且支持多线程。

      System.Web.Configuration.WebConfigurationManager 的其他方法与 ASP.NET 的配置有关。我认为System.Web.Configuration.WebConfigurationManagerConfigurationManager 的扩展,甚至是对网络生态系统的适应。

      所以,你应该使用通用的System.Configuration.ConfigurationManager

      猜你喜欢
      • 1970-01-01
      • 2010-09-07
      • 2013-03-08
      • 2012-08-25
      • 2011-12-03
      • 2014-03-03
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多