代码之家  ›  专栏  ›  技术社区  ›  Eran Betzalel

如何使用IConfigurationSectionHandler进行全局配置?

  •  2
  • Eran Betzalel  · 技术社区  · 14 年前

    当它读当地报纸的时候web.config文件当然,问题是读取全局配置文件(根目录)web.config文件)由共享设置组成的。

    3 回复  |  直到 14 年前
        1
  •  4
  •   Chris Taylor    14 年前

    为了正确地回答您的问题,我们可能需要查看来自您的自定义节处理程序的一些代码。

    但是,有一点会立即浮现在脑海中,那就是您可能没有正确地使用 parent 正在传递给处理程序的参数。

    IConfigurationSectionHandler.Create ,和 configuration object 它是从“Create”方法返回的对象。

    非常简单地说,你的分区处理程序 Create 方法的层次结构中的自定义节的每次出现都应调用web.config文件文件夹。每次调用时,您从中返回的上一个配置对象 作为 参数,当然第一个调用将有一个 null 表示需要创建此初始“配置对象”的父级,后续调用不应创建新的 configuration objec 但添加或修改作为父级传入的。

    最终结果是,当您从文件中读取配置时,会收到一个“configuration object”,其中包含所有级别的设置之和。

    注意: 你真的应该用 ConfigurationSection ,因为自Framework 2.0以来,IConfigurationSectionHandler一直被弃用。这里有一个使用这个类的链接。

    http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

        2
  •  4
  •   Aliostad    14 年前

    我更喜欢继承ConfigurationSection而不是实现IConfigurationSectionHandler,因为您不必手动处理XML。

    http://support.microsoft.com/kb/309045

    http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

        3
  •  3
  •   Venugopal M    10 年前

    这是在中使用我们自己的配置类的示例web.config文件.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml;
    
    namespace MyProject.MyConfigSection
    {
        public class MyConfig:System.Configuration.IConfigurationSectionHandler
        {
            public int MyNum1 { get; set; }
            public int MyNum2 { get; set; }
            public int MyNum3 { get; set; }
    
        public MyConfig()
        {
            MyNum1 = 0;
            MyNum2 = 0;
            MyNum3 = 0;
        }
    
    //implement interface member
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            try
            {
                MyConfig options = new MyConfig();
                if (section == null) return options;
    
                foreach (XmlNode node in section.ChildNodes)
                {
                    if (node.Name == "MyNum1")
                        options.MyNum1 = int.Parse(node.InnerText);
                    else if (node.Name == "MyNum2")
                        options.MyNum2 = int.Parse(node.InnerText);
                    else if (node.Name == "MyNum3")
                        options.MyNum3 = int.Parse(node.InnerText);
                }
                return options;
            }
            catch (Exception ex)
            {
                throw new System.Configuration.ConfigurationException(
                 "Error loading startup default options", ex, section);
            }
    
        }
    }
    

    }

    <configuration>
        <configSections>
            <section name="MYTESTCONFIGSECTION" type="MyProject.MyConfigSection.MyConfig" />
    .... //other sections
    .... //other sections
        </configSections>
    

    现在在web.config文件本身,我们在配置标签之间的任意位置添加:

    <MYTESTCONFIGSECTION>
        <MyNum1>111</MyNum1>
        <MyNum2>222</MyNum2>
        <MyNum3>333</MyNum3>
    </MYTESTCONFIGSECTION>
    </configuration>
    

    var myconfig = System.Web.Configuration.WebConfigurationManager.GetSection("MYTESTCONFIGSECTION") as MyConfigSection.MyConfig;
        myconfig.MyNum1;
        myconfig.MyNum2;
        myconfig.MyNum3;
    

    希望这对别人有帮助。