【问题标题】:How do I define custom web.config sections with potential child elements and attributes for the properties?如何定义具有潜在子元素和属性的属性的自定义 web.config 部分?
【发布时间】:2023-03-26 18:41:01
【问题描述】:

我开发的 Web 应用程序通常需要相互依赖的配置设置,而且当我们在每个环境之间移动时,也有一些设置必须更改。

我们所有的设置目前都是简单的键值对,但创建自定义配置部分会很有用,这样当两个值需要一起更改或需要针对环境更改设置时,它会很明显。

创建自定义配置部分的最佳方式是什么?在检索这些值时是否需要特别注意?

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    您可以使用部分处理程序来完成此操作。在http://www.codeproject.com/KB/aspnet/ConfigSections.aspx 有一个关于如何编写一个的基本概述,但是它指的是 app.config,这与编写一个用于 web.config 的内容几乎相同。这将允许您在配置文件中拥有自己的 XML 树并进行一些更高级的配置。

    【讨论】:

      【解决方案2】:

      快点脏:

      首先创建您的 ConfigurationSectionConfigurationElement 类:

      public class MyStuffSection : ConfigurationSection
      {
          ConfigurationProperty _MyStuffElement;
      
          public MyStuffSection()
          {
              _MyStuffElement = new ConfigurationProperty("MyStuff", typeof(MyStuffElement), null);
      
              this.Properties.Add(_MyStuffElement);
          }
      
          public MyStuffElement MyStuff
          {
              get
              {
                  return this[_MyStuffElement] as MyStuffElement;
              }
          }
      }
      
      public class MyStuffElement : ConfigurationElement
      {
          ConfigurationProperty _SomeStuff;
      
          public MyStuffElement()
          {
              _SomeStuff = new ConfigurationProperty("SomeStuff", typeof(string), "<UNDEFINED>");
      
              this.Properties.Add(_SomeStuff);
          }
      
          public string SomeStuff
          {
              get
              {
                  return (String)this[_SomeStuff];
              }
          }
      }
      

      然后让框架知道如何在 web.config 中处理你的配置类:

      <configuration>
        <configSections>
          <section name="MyStuffSection" type="MyWeb.Configuration.MyStuffSection" />
        </configSections>
        ...
      

      实际上在下面添加您自己的部分:

        <MyStuffSection>
          <MyStuff SomeStuff="Hey There!" />
        </MyStuffSection>
      

      然后你可以在你的代码中使用它:

      MyWeb.Configuration.MyStuffSection configSection = ConfigurationManager.GetSection("MyStuffSection") as MyWeb.Configuration.MyStuffSection;
      
      if (configSection != null && configSection.MyStuff != null)
      {
          Response.Write(configSection.MyStuff.SomeStuff);
      }
      

      【讨论】:

        【解决方案3】:

        使用属性、子配置部分和约束

        还可以使用自动处理管道的属性,以及提供轻松添加约束的能力。

        我在这里提供一个示例,来自我自己在我的一个网站中使用的代码。有一个限制,我规定了任何一个用户可以使用的最大磁盘空间量。

        MailCenterConfiguration.cs:

        namespace Ani {
        
            public sealed class MailCenterConfiguration : ConfigurationSection
            {
                [ConfigurationProperty("userDiskSpace", IsRequired = true)]
                [IntegerValidator(MinValue = 0, MaxValue = 1000000)]
                public int UserDiskSpace
                {
                    get { return (int)base["userDiskSpace"]; }
                    set { base["userDiskSpace"] = value; }
                }
            }
        }
        

        这是在 web.config 中这样设置的

        <configSections>
            <!-- Mailcenter configuration file -->
            <section name="mailCenter" type="Ani.MailCenterConfiguration" requirePermission="false"/>
        </configSections>
        ...
        <mailCenter userDiskSpace="25000">
            <mail
             host="my.hostname.com"
             port="366" />
        </mailCenter>
        

        子元素

        子 xml 元素 mail 是在与上述相同的 .cs 文件中创建的。在这里,我在端口上添加了约束。如果为端口分配的值不在此范围内,则运行时将在加载配置时报错。

        MailCenterConfiguration.cs:

        public sealed class MailCenterConfiguration : ConfigurationSection
        {
            [ConfigurationProperty("mail", IsRequired=true)]
            public MailElement Mail
            {
                get { return (MailElement)base["mail"]; }
                set { base["mail"] = value; }
            }
        
            public class MailElement : ConfigurationElement
            {
                [ConfigurationProperty("host", IsRequired = true)]
                public string Host
                {
                    get { return (string)base["host"]; }
                    set { base["host"] = value; }
                }
        
                [ConfigurationProperty("port", IsRequired = true)]
                [IntegerValidator(MinValue = 0, MaxValue = 65535)]
                public int Port
                {
                    get { return (int)base["port"]; }
                    set { base["port"] = value; }
                }
        

        使用

        要在代码中实际使用它,您所要做的就是实例化 MailCenterConfigurationObject,这将自动从 web.config 中读取相关部分。

        MailCenterConfiguration.cs

        private static MailCenterConfiguration instance = null;
        public static MailCenterConfiguration Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = (MailCenterConfiguration)WebConfigurationManager.GetSection("mailCenter");
                }
        
                return instance;
            }
        }
        

        另一个文件.cs

        public void SendMail()
        {
            MailCenterConfiguration conf = MailCenterConfiguration.Instance;
            SmtpClient smtpClient = new SmtpClient(conf.Mail.Host, conf.Mail.Port);
        }
        

        检查有效性

        我之前提到过,当加载配置并且某些数据不符合您设置的规则(例如在 MailCenterConfiguration.cs 中)时,运行时会抱怨。当我的网站启动时,我倾向于尽快了解这些事情。解决此问题的一种方法是在 _Global.asax.cx.Application_Start_ 中加载配置,如果配置无效,您将收到异常通知。您的网站不会启动,而是会在Yellow screen of death 中向您提供详细的异常信息。

        Global.asax.cs

        protected void Application_ Start(object sender, EventArgs e)
        {
            MailCenterConfiguration.Instance;
        }
        

        【讨论】:

        • 如果您希望有多个 子元素,您将如何更改代码?
        【解决方案4】:

        自定义配置非常方便,应用程序通常最终需要可扩展的解决方案。

        .NET 1.1请参考文章http://aspnet.4guysfromrolla.com/articles/020707-1.aspx

        注意:上述解决方案也适用于 .NET 2.0。

        .NET 2.0具体解决方案请参考文章http://aspnet.4guysfromrolla.com/articles/032807-1.aspx

        【讨论】:

          【解决方案5】:

          有一个出色的example on MSDN 使用ConfigurationCollection 和.NET 4.5 用于web.config 中具有配置项列表的自定义部分。

          【讨论】:

            【解决方案6】:

            我发现的最简单的方法是使用appSettings section

            1. 将以下内容添加到 Web.config:

              <appSettings>
                  <add key="MyProp" value="MyVal"/>
              </appSettings>
              
            2. 从您的代码访问

              NameValueCollection appSettings = ConfigurationManager.AppSettings;
              string myPropVal = appSettings["MyProp"];
              

            【讨论】:

              猜你喜欢
              • 2010-09-05
              • 1970-01-01
              • 1970-01-01
              • 2019-08-12
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多