你好,欢迎来到电脑编程技巧与维护杂志社! 杂志社简介广告服务读者反馈编程社区  
合订本订阅
 
 
您的位置:技术专栏 / 数据库开发
ASP.NET2.0中创建自定义配置节处理程序
 
在ASP.NET中,配置数据存储在web.config文件中。该文件使用xml来表示数据,所有的配置信息都位于根xml标记之间。这里的配置信息分为两个区域:配置节处理程序声明区域和配置节设置区域。

配置节处理程序声明区域位于xml标记之间,使用section元素来声明配置节处理程序,可以将这些配置节处理程序声明嵌套在sectionGroup元素中,帮助组织配置信息,如下所示:

<configSections>

 <sectionGroup name="system.web" type=
"System.Web.Configuration.SystemWebSectionGroup, 
System.Web, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a">
     <section name="pages" type=
"System.Web.Configuration.PagesSection, 
System.Web, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a" />
     <!-- Other <section /> elements. -->
 </sectionGroup>
<!-- Other <sectionGroup /> and <section /> elements. -->
</configSections>

配置节处理程序声明区域中的每个配置节都有一个节处理程序声明,节处理程序是实现了ConfigurationSection类的.Net Framework类。节处理程序声明中包含节的名称(如pages)以及用来处理该节中配置数据的节处理程序类的名称(如System.Web.Configuration,PagesSection)。如下所示:

<section name="pages"   type=
"System.Web.Configuration.PagesSection, 
System.Web, Version=2.0.0.0, Culture=neutral, 
PublicKeyToken=b03f5f7f11d50a3a">
</section>

当您需要自定义配置节时,需要完成这样几个任务:

一,设计自己的配置节处理程序类,实现ConfigurationSection类,并扩展自己所需的功能;

二,在配置节处理程序声明区域中声明节处理程序类;

三,在配置节配置自定义的配置节。

实现自己的配置节处理程序通常有两种方式,也就是两种实现模型,分别是声明性模型和编程模型。首先我们以声明性模型来实现一个自定义配置节处理程序。添加CustomSection.cs文件到网站,其内容如下:

using System;
using System.Configuration;
namespace AntarDev
{
    public class CustomSection:ConfigurationSection
    {
        public CustomSection()
        {   }

        [ConfigurationProperty("CustomAttribute")]
        public String CustomAttribute
        {
            get
            {
                return (String)this["CustomAttribute"];
            }
            set
            {
                this["CustomAttribute"] = value;
            }
        }
    }
}

修改web.config文件,在ConfigSections配置节,添加自己的配置节。如下所示:

<configSections>
    <sectionGroup  name="newSectionGroup">
        <section  name="newSection"  type="AntarDev.CustomSection"/>
    </sectionGroup>
</configSections>

在之后的配置节设置区域中,使用我们新定义的配置节。如下所示:

<newSectionGroup>
    <newSection CustomAttribute="AttributeValue" />
</newSectionGroup>

然后我们在Default.aspx的Page_Load事件中书写如下代码,以编程方式访问配置数据:

protected void Page_Load(object sender, EventArgs e)
{
     AntarDev.CustomSection cus = 
(AntarDev.CustomSection)ConfigurationManager.
GetSection("newSectionGroup/newSection");
     Response.Write
("Attribute=" + cus.CustomAttribute);
}

运行网站,将会输出Attribute=AttributeValue,我们的自定义配置节已经可以正常工作,虽然她是那么的原始,而且看起来并不能完成什么具体的工作,但是先让我们耐心理解这个简单的示例吧,这样我们才能进一步学些自定义配置节的高级用法,来帮助我们完成有意义的工作。^_^

在CustomSection类的实现中有下面几个细节需要解释一下:

1.继承自ConfigurationSection类。这是本文中创建自定义配置节程序类的基础。虽然也可以通过继承IConfigurationSectionHandler创建自定义配置节,但是该接口在.Net2.0中已经被否决,本文中将不讨论基于该接口的实现方法。

2.通过对类的一个普通字符串属性(Property)CustomAttribute进行修饰,使之成为了一个配置节的节属性(Attribute),这是通过ConfigurationPropertyAttribute来实现的。关于该属性(Attribute)的详细用法,请查阅MSDN。

3.在CustomAttribute属性的get和set访问器中,我们使用了this["CustomAttribute "]来存取节属性的值。为什么可以这样使用呢?因为我们的类继承自ConfigurationSection类,而该类继承自ConfigurationElement,在ConfigurationElement类中定义了两个索引器,如下所示:

protected internal object this[ConfigurationProperty prop] { get; set; }

protected internal object this[string propertyName] { get; set; }

我们自己的程序中的用法,正是使用的该类中的第二个索引器,对配置数据进行访问。通过声明性模型创建自定义配置节程序类比较简单,因为他对ASP.NET的基础配置架构进行了进一步的封装,使我们更方便使用,同时简化了程序结构。

4.[ConfigurationProperty("CustomAttribute")]属性修饰与被修饰属性的两个访问器中的this["CustomAttribute"],其中的字符串必须一致,因为这个字符串就代表着要操作的节属性名称,也就是在web.config中要使用的节属性名称。

通过上例中的CustomSection类,我们实现了如下形式的自定义配置节:

<newSectionGroup>
   <newSection   CustomAttribute="AttributeValue" />
</newSectionGroup>

现在,newSection元素仅仅是一个元素,如果我们希望newSection元素可以包含子元素呢?如下所示:

<newSectionGroup>
   <newSection  CustomAttribute="AttributeValue">
       <SubElement  ElementAttribute1="newValue"></SubElement>
    </newSection>
</newSectionGroup>

要能够给newSection元素添加一个子元素,需要对我们的自定义配置节程序类进行改进,如下所示:

using System;
using System.Configuration;
namespace AntarDev
{
    public class CustomSection:ConfigurationSection
    {
        public CustomSection()
        {   }

        [ConfigurationProperty("CustomAttribute")]
        public String CustomAttribute
        {
            get
            {
                return (String)this["CustomAttribute"];
            }
            set
           {
                this["CustomAttribute"] = value;
            }
        }

        [ConfigurationProperty("SubElement")]
        public CustomElement SubElement
        {//新添加的属性,
因为其返回类型继承自ConfigurationElement,
故可以在web.config中作为配置节的子元素使用。
            get
            { return (CustomElement)this["SubElement"];}
            set
            { this["SubElement"] = value; }
        }
    }

public class CustomElement : ConfigurationElement
 {//新添加的自定义元素

        [ConfigurationProperty("ElementAttribute1")]
        public string ElementAttribute1
        {
            get
            { return (String)this["ElementAttribute1"]; }
            set
            { this["ElementAttribute1"] = value; }
        }
    }
}

在上述程序中,我们进行了如下改动。

1.增加一个继承自ConfigurationElement的类CustomElement,作为自定义配置节的子元素,同时为该类声明一个新属性,作为子元素的元素属性。

2.在自定义配置节程序类中增加一个属性,该属性使用ConfigurationPropertyAttribute修饰,同时返回类型为CustomElement(继承自ConfigurationElement)。

编程访问新增的子元素。

protected void Page_Load
(object sender, EventArgs e)
{
    AntarDev.CustomSection cus = 
AntarDev.CustomSection)ConfigurationManager.
GetSection("newSectionGroup/newSection");
    Response.Write("subElement=
" + cus.SubElement.ElementAttribute1);
}

如果希望配置节有两个或者两个以上子元素呢?类似于下面这样:

<newSectionGroup>
    <newSection   CustomAttribute="AttributeValue">
        <SubElement   ElementAttribute1="newValue" />
        <SubElement   ElementAttribute1="newValue" />
    </newSection>
</newSectionGroup>

直接编译运行,会出现错误,提示SubElement只能出现一次,因为该子元素在配置节处理程序中是作为一个属性存在(Property)的,如果希望出现多个子元素,需要使用集合的方式来实现。如下所示:

<newSectionGroup>
 <newSection  CustomAttribute="AttributeValue">
    <ElementCollection>
      <add  ElementAttribute1="CollectionTest" />
      <add  ElementAttribute1="CollectionTest2" />
    </ElementCollection>
 </newSection>
</newSectionGroup>

为此,需要对我们的自定义配置节程序类进行改进,如下所示:

using System;
using System.Configuration;
namespace AntarDev
{
    public class CustomSection:ConfigurationSection
    {
        public CustomSection()
        {   }

        [ConfigurationProperty("CustomAttribute")]
        public String CustomAttribute
        {
            get
            {
                return (String)this["CustomAttribute"];
            }
            set
            {
                this["CustomAttribute"] = value;
            }
        }

        [ConfigurationProperty("SubElement")]
        public CustomElement SubElement
        {
            get
            { return (CustomElement)this["SubElement"];}
            set
            { this["SubElement"] = value; }
        }

        [ConfigurationProperty("ElementCollection")]
        public CustomElementCollection SubElementCollection
        {//添加了一个新属性,返回类型为一个继承自ConfigurationElementCollection的类
            get
            { return (CustomElementCollection)this["ElementCollection"];}
        }
    }

    public class CustomElement : ConfigurationElement
    {
        [ConfigurationProperty("ElementAttribute1")]
        public string ElementAttribute1
        {
            get
            { return (String)this["ElementAttribute1"]; }
            set
            { this["ElementAttribute1"] = value; }
        }
    }

    public class CustomElementCollection : ConfigurationElementCollection
    {//新创建的类,下面两个方法是继承自ConfigurationElementCollection的类必须实现的两个基本方法
        protected override ConfigurationElement CreateNewElement()
        {
            return new CustomElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((CustomElement)element).ElementAttribute1;
        }
    }
}

考虑一下在appSettings配置节中常见的形式,并不需要使用一个嵌套元素将子元素包围起来,直接就可以使用add,remove,clear元素对配置元素进行修改。在我们的自定义配置节处理程序中如何实现呢?先看一下原先的实现方法:

[ConfigurationProperty("ElementCollection")]
public CustomElementCollection SubElementCollection
{//添加了一个新属性,返回类型为一个继承自ConfigurationElementCollection的类
     get{ return (CustomElementCollection)this["ElementCollection"];}
}

我们使用ConfigurationPropertyAttribute修饰属性,将其转换为一个内置的元素集合,如果将ConfigurationPropertyAttribute中的字符串改为空,同时增加,IsDefaultCollection=true,变成下面这样:

[ConfigurationProperty("",IsDefaultCollection=true)]
public CustomElementCollection SubElementCollection
{//添加了一个新属性,返回类型为一个继承自ConfigurationElementCollection的类
     get{ return (CustomElementCollection)this[""];}//这里也为空字符串
}

这样的话,就可以像appSettings配置节那样直接操作子元素了。至于newSectionGroup元素,如果不喜欢,去掉就是了。

<newSectionGroup>
    <newSection    CustomAttribute="AttributeValue">
      <add    ElementAttribute1="directCollection" />
      <add    ElementAttribute1="directCollection2" />
    </newSection>
 </newSectionGroup>

至此,关于自定义配置节处理程序中,常见的几种元素组织形式,我们都已经可以实现。细节部分,在具体的项目中还需要具体完善。同时.NET Framework中有不少现成的配置节处理程序,如果能够实现需求,不放直接使用,比如NameValueSectionHandler等类。

  推荐精品文章

·2024年12月目录 
·2024年11月目录 
·2024年10月目录 
·2024年9月目录 
·2024年8月目录 
·2024年7月目录 
·2024年6月目录 
·2024年5月目录 
·2024年4月目录 
·2024年3月目录 
·2024年2月目录 
·2024年1月目录
·2023年12月目录
·2023年11月目录

  联系方式
TEL:010-82561037
Fax: 010-82561614
QQ: 100164630
Mail:gaojian@comprg.com.cn

  友情链接
 
Copyright 2001-2010, www.comprg.com.cn, All Rights Reserved
京ICP备14022230号-1,电话/传真:010-82561037 82561614 ,Mail:gaojian@comprg.com.cn
地址:北京市海淀区远大路20号宝蓝大厦E座704,邮编:100089