在Web.config或App.config中的添加自定义配置

简介: .Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持。最近看到一些项目中还在自定义xml文件做程序的配置,所以忍不住写一篇用系统自定义配置的随笔了。

.Net中的System.Configuration命名空间为我们在web.config或者app.config中自定义配置提供了完美的支持。最近看到一些项目中还在自定义xml文件做程序的配置,所以忍不住写一篇用系统自定义配置的随笔了。

如果你已经对自定义配置了如指掌,请忽略这篇文章。

言归正传,我们先来看一个最简单的自定义配置

<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
   < configSections >
     < section  name="simple" type="ConfigExample.Configuration.SimpleSection,ConfigExample"/>
   </ configSections >
   < simple  maxValue="20" minValue="1"></ simple >
</ configuration >

在配置文件中使用自定义配置,需要在configSections中添加一个section元素,并制定此section元素对应的类型和名字。然后再在configuration根节点下面添加此自定义配置,如上例中的simple节点。simple节点只有两个整形数的属性maxValue和minValue。

要在程序中使用自定义配置我们还需要实现存取这个配置块的类型,一般需要做如下三件事:
1. 定义类型从System.Configuration.ConfigurationSection继承
2. 定义配置类的属性,这些属性需要用ConfigurationProperty特性修饰,并制定属性在配置节中的名称和其他一些限制信息
3. 通过基类的string索引器实现属性的get ,set

非常简单和自然,如下是上面配置类的实现:

public  class  SimpleSection:System.Configuration.ConfigurationSection
{
     [ConfigurationProperty( "maxValue" ,IsRequired= false ,DefaultValue=Int32.MaxValue)]
     public  int  MaxValue
     {
         get
         {
             return   ( int ) base [ "maxValue" ];
         }
         set
         {
             base [ "maxValue" ] = value;
         }
     }
 
     [ConfigurationProperty( "minValue" ,IsRequired= false ,DefaultValue=1)]
     public  int  MinValue
     {
         get  { return  ( int ) base [ "minValue" ];}
         set  { base [ "minValue" ] = value; }
     }
 
 
     [ConfigurationProperty( "enabled" ,IsRequired= false ,DefaultValue= true )]
     public  bool  Enable
     {
         get
         {
             return  ( bool ) base [ "enabled" ];
         }
         set
         {
             base [ "enabled" ] = value;
         }
     }
}

这样子一个简单的配置类就完成了,怎么在程序中使用这个配置呢?需要使用ConfigurationManager类(要引用System.configuration.dll这个dll只有在.Net2.0之后的版本中才有)的GetSection方法获得配置就可以了。如下代码:

SimpleSection simple = ConfigurationManager.GetSection( "simple" ) as  SimpleSection;
Console.WriteLine( "simple minValue={0} maxValue = {1}" ,simple.MinValue,simple.MaxValue);

这个配置类太过简陋了,可能有时候我们还需要更复杂的构造,比如在配置类中使用类表示一组数据,下面我们看一个稍微复杂一点的自定义配置

<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
   < configSections >
     < section  name="complex" type="ConfigExample.Configuration.ComplexSection,ConfigExample"/>
   </ configSections >
   < complex  height="190">
     < child  firstName="James" lastName="Bond"/>
   </ complex >
</ configuration >

这个配置的名字是complex,他有一个属性height,他的节点内还有一个child元素这个元素有两个属性firstName和lastName;对于这个内嵌的节点该如何实现呢?首先我们需要定义一个类,要从ConfigurationElement类继承,然后再用和SimpleSection类似的方法定义一些用ConfigurationProperty特性修饰的属性就可以了,当然属性值的get,set也要使用基类的索引器。如下实现:

public  class  ComplexSection : ConfigurationSection
{
     [ConfigurationProperty( "height" , IsRequired = true )]
     public  int  Height
     {
         get
         {
             return  ( int ) base [ "height" ];
         }
         set
         {
             base [ "height" ] = value;
         }
     }
 
     [ConfigurationProperty( "child" , IsDefaultCollection = false )]
     public  ChildSection Child
     {
         get
         {
             return  (ChildSection) base [ "child" ];
         }
         set
         {
             base [ "child" ] = value;
         }
     }
}
 
public  class  ChildSection : ConfigurationElement
{
     [ConfigurationProperty( "firstName" , IsRequired = true , IsKey = true )]
     public  string  FirstName
     {
         get
         {
             return  ( string ) base [ "firstName" ];
         }
         set
         {
             base [ "firstName" ] = value;
         }
     }
 
     [ConfigurationProperty( "lastName" , IsRequired = true )]
     public  string  LastName
     {
         get
         {
             return  ( string ) base [ "lastName" ];
         }
         set
         {
             base [ "lastName" ] = value;
         }
     }
}

还有稍微再复杂一点的情况,我们可能要在配置中配置一组相同类型的节点,也就是一组节点的集合。如下面的配置:

<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
   < configSections >
     < section  name="complex" type="ConfigExample.Configuration.ComplexSection,ConfigExample"/>
   </ configSections >
   < complex  height="190">
     < child  firstName="James" lastName="Bond"/>
 
     < children >
       < add  firstName="Zhao" lastName="yukai"/>
       < add  firstName="Lee" lastName="yukai"/>
       < remove  firstName="Zhao"/>
     </ children >
   </ complex >
</ configuration >

请看children节点,它就是一个集合类,在它里面定义了一组add元素,也可以有remove节点把已经添进去的配置去掉。

要使用自定义节点集合需要从ConfigurationElementCollection类继承一个自定义类,然后要实现此类GetElementKey(ConfigurationElement element)和ConfigurationElement CreateNewElement()两个方法;为了方便的访问子节点可以在这个类里面定义只读的索引器。请看下面的实现

public  class  Children : ConfigurationElementCollection
{
     protected  override  object  GetElementKey(ConfigurationElement element)
     {
         return  ((ChildSection)element).FirstName;
     }
 
     protected  override  ConfigurationElement CreateNewElement()
     {
         return  new  ChildSection();
     }
 
     public  ChildSection this [ int  i]
     {
         get
         {
             return  (ChildSection) base .BaseGet(i);
         }
     }
 
     public  ChildSection this [ string  key]
     {
         get
         {
             return  (ChildSection) base .BaseGet(key);
         }
     }
 
}

当然要使用此集合类我们必须在Complex类中添加一个此集合类的属性,并要指定集合类的元素类型等属性,如下:

[ConfigurationProperty( "children" , IsDefaultCollection = false )]
     [ConfigurationCollection( typeof (ChildSection), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove" )]
     public  Children Children
     {
         get
         {
             return  (Children) base [ "children" ];
         }
         set
         {
             base [ "children" ] = value;
         }
}

我们会经常用到类似appSettings配置节的键值对的构造,这时候我们就不必再自己实现了,我们可以直接使用现有的System.Configuration.NameValueConfigurationCollection类来定义一个自定义的键值对。可以在Complex类中定义如下属性

[ConfigurationProperty( "NVs" , IsDefaultCollection = false )]
     public  System.Configuration.NameValueConfigurationCollection NVs
     {
         get
         {
             return  (NameValueConfigurationCollection) base [ "NVs" ];
         }
         set
         {
             base [ "NVs" ] = value;
         }
}

然后在配置文件的complex节中添加键值对配置

< NVs >
     < add  name="abc" value="123"/>
     < add  name="abcd" value="12d3"/>
</ NVs >

到这儿已经基本上可以满足所有的配置需求了。不过还有一点更大但是不复杂的概念,就是sectionGroup。我们可以自定义SectionGroup,然后在sectionGroup中配置多个section;分组对于大的应用程序是很有意义的。

如下配置,配置了一个包含simple和一个complex两个section的sectionGroup

<? xml  version="1.0" encoding="utf-8" ?>
< configuration >
   < configSections >
     < sectionGroup  type="ConfigExample.Configuration.SampleSectionGroup,ConfigExample" name="sampleGroup">
       < section  type="ConfigExample.Configuration.SimpleSection,ConfigExample" allowDefinition="Everywhere" name="simple" />
       < section  type="ConfigExample.Configuration.ComplexSection,ConfigExample" allowDefinition="Everywhere" name="complex"/>
     </ sectionGroup >
   </ configSections >
   < sampleGroup >
     < simple  maxValue="20" minValue="1">
     </ simple >
 
     < complex  height="190">
       < child  firstName="James" lastName="Bond"/>
       < children >
         < add  firstName="Zhao" lastName="yukai"/>
         < add  firstName="Lee" lastName="yukai"/>
         < remove  firstName="Zhao"/>
       </ children >
   < NVs >
     < add  name="abc" value="123"/>
     < add  name="abcd" value="12d3"/>
   </ NVs >
     </ complex >
   </ sampleGroup >
</ configuration >

为了方便的存取sectionGroup中的section我们可以实现一个继承自System.Configuration.ConfigurationSectionGroup类的自定义类。实现很简单,就是通过基类的Sections[“sectionName”]索引器返回Section。如下:

public  class  SampleSectionGroup : System.Configuration.ConfigurationSectionGroup
{
     public  SimpleSection Simple
     {
         get
         {
             return  (SimpleSection) base .Sections[ "simple" ];
         }
     }
 
     public  ComplexSection Complex
     {
         get
         {
             return  (ComplexSection) base .Sections[ "complex" ];
         }
     }
}

需要注意的是SectionGroup不能使用ConfigurationManager.GetSection(string)方法来获得,要获得sectionGroup必须通过Configuration类的SectionGroups[string]索引器获得,如下示例代码:

SampleSectionGroup sample = (SampleSectionGroup)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups[ "sampleGroup" ];

总结:

.Net framework给我们提供了一套很方便的配置库,我们只需要继承对应的类简单的配置一下就可以方便的使用在web.config或者app.config中配置的自定义节点了。

 

自定义配置文件节源码



来自:http://www.cnblogs.com/yukaizhao/archive/2011/12/02/net-web-config-costom-config-implement.html

相关文章
|
2月前
|
算法 Java Go
【GoGin】(1)上手Go Gin 基于Go语言开发的Web框架,本文介绍了各种路由的配置信息;包含各场景下请求参数的基本传入接收
gin 框架中采用的路优酷是基于httprouter做的是一个高性能的 HTTP 请求路由器,适用于 Go 语言。它的设计目标是提供高效的路由匹配和低内存占用,特别适合需要高性能和简单路由的应用场景。
263 4
|
5月前
|
域名解析 网络协议 API
【Azure Container App】配置容器应用的缩放规则 Managed Identity 连接中国区 Azure Service Bus 问题
本文介绍了在 Azure Container Apps 中配置基于自定义 Azure Service Bus 的自动缩放规则时,因未指定云环境导致的域名解析错误问题。解决方案是在扩展规则中添加 `cloud=AzureChinaCloud` 参数,以适配中国区 Azure 环境。内容涵盖问题描述、原因分析、解决方法及配置示例,适用于使用 KEDA 实现事件驱动自动缩放的场景。
144 1
|
7月前
|
Android开发 数据安全/隐私保护 开发者
Android自定义view之模仿登录界面文本输入框(华为云APP)
本文介绍了一款自定义输入框的实现,包含静态效果、hint值浮动动画及功能扩展。通过组合多个控件完成界面布局,使用TranslateAnimation与AlphaAnimation实现hint文字上下浮动效果,支持密码加密解密显示、去除键盘回车空格输入、光标定位等功能。代码基于Android平台,提供完整源码与attrs配置,方便复用与定制。希望对开发者有所帮助。
136 0
|
8月前
|
人工智能 JSON 小程序
【一步步开发AI运动APP】七、自定义姿态动作识别检测——之规则配置检测
本文介绍了如何通过【一步步开发AI运动APP】系列博文,利用自定义姿态识别检测技术开发高性能的AI运动应用。核心内容包括:1) 自定义姿态识别检测,满足人像入镜、动作开始/停止等需求;2) Pose-Calc引擎详解,支持角度匹配、逻辑运算等多种人体分析规则;3) 姿态检测规则编写与执行方法;4) 完整示例展示左右手平举姿态检测。通过这些技术,开发者可轻松实现定制化运动分析功能。
|
2月前
|
人工智能 小程序 搜索推荐
【一步步开发AI运动APP】十二、自定义扩展新运动项目2
本文介绍如何基于uni-app运动识别插件实现“双手并举”自定义扩展运动,涵盖动作拆解、姿态检测规则构建及运动分析器代码实现,助力开发者打造个性化AI运动APP。
|
5月前
|
存储 Linux Apache
在CentOS上配置SVN至Web目录的自动同步
通过上述配置,每次当SVN仓库中提交新的更改时,`post-commit`钩子将被触发,SVN仓库的内容会自动同步到指定的Web目录,从而实现代码的连续部署。
191 16
|
7月前
|
人工智能 小程序 API
【一步步开发AI运动APP】九、自定义姿态动作识别检测——之关键点追踪
本文介绍了【一步步开发AI运动APP】系列中的关键点追踪技术。此前分享的系列博文助力开发者打造了多种AI健身场景的小程序,而新系列将聚焦性能更优的AI运动APP开发。文章重点讲解了“关键点位变化追踪”能力,适用于动态运动(如跳跃)分析,弥补了静态姿态检测的不足。通过`pose-calc`插件,开发者可设置关键点(如鼻子)、追踪方向(X或Y轴)及变化幅度。示例代码展示了如何在`uni-app`框架中使用`createPointTracker`实现关键点追踪,并结合人体识别结果完成动态分析。具体实现可参考文档与Demo示例。
|
6月前
《仿盒马》app开发技术分享-- 自定义标题栏&商品详情初探(9)
上一节我们实现了顶部toolbar的地址选择,会员码展示,首页的静态页面就先告一段落,这节我们来实现商品列表item的点击传值、自定义标题栏。
86 0
|
6月前
《仿盒马》app开发技术分享-- 首页活动配置(5)
上一篇文章中我们实现了项目端云一体化首页部分模块动态配置,实现了对模块模块的后端控制显示和隐藏,这能让我们的app更加的灵活,也能应对更多的情况。现在我们来对配置模块进行完善,除了已有的模块以外,我们还有一些banner ,活动入口等模块,这些模块的数据并不多,所以我们也归纳到配置中去实现。并且我们在配置表中添加了一些不同的id,我们只需要根据相对应的id 去查询对应的表就可以了
121 0