您的位置:首页 > 移动开发

在web.config和app.config文件中增加自定义配置节点的方法

2016-05-31 14:53 926 查看
一
本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下

.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中配置的自定义节点了



有经验的开发人员都知道在开发.NET应用时可以利用配置文件保存一些常用并且有可能变化的信息,例如日志文件的保存路径、数据库连接信息等等,这样即使生产环境中的参数信息与开发环境不一致也只需要更改配置文件而不用改动源代码再重新编译,极其方便。并且我们一般还约定,在<appSettings>节点保存应用程序的配置信息,在<connectionStrings>中保存数据库连接字符串信息。上面的这些方法和约定足以让我们在大部分开发中获得方便,但是在有些情况下有些配置信息可以按组分类存放,如果采用上面的方法不仅不直观,而且读取起来也不是太方便,幸好在.NET里就提供了这样的方法。如果有使用过Log4Net或者Enyim.Caching的朋友,肯定对下面的配置不会陌生:

<sectionGroup name="enyim.com"><section name="memcached"

type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" /></sectionGroup>

或:

<configSections><section name="log4net" type="System.Configuration.IgnoreSectionHandler"/></configSections>

在出现上面配置的配置文件中,我们就会找到名称为"enyim.com"或者"log4net"的节点,尽管它们本不属于config文件的默认节点,但是通过上面的配置之后程序运行并不会报错。这样一来,相关配置信息也可以很好分类保存起来。

在这里我演示一个简单的例子,这个例子来源于我的一个从2006年起就开始开发的自用软件(因为没有美化所以没有免费发布),在这个应用程序的connfig文件中我增加了一些特有的配置,所以新增了一个自己的节点,app.config文件内容如下:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="SoftwareSettings" type="ImageAssistant.Configuration.SoftwareSettings, ImageAssistant" />

</configSections>

<SoftwareSettings>

<LoadSettings>

<add key="LoadBmp" value="true"/>

<add key="LoadJpg" value="true"/>

<add key="LoadGif" value="true"/>

<add key="LoadPng" value="false"/>

</LoadSettings>

<PathSettings SavePath="C:\ResizeImages\" SearchSubPath="true"/>

</SoftwareSettings>

<appSettings>

<add key="LoadBmp" value="true"/>

<add key="LoadJpg" value="true"/>

<add key="LoadGif" value="true"/>

<add key="LoadPng" value="false"/>

<add key="IncludeSubPath" value="true"/>

</appSettings>

</configuration>

在config文件中我们使用<section name="SoftwareSettings" type="ImageAssistant.Configuration.SoftwareSettings, ImageAssistant" />告诉应用程序对于配置文件中的SoftwareSettings节点,其对应的类是ImageAssistant程序集中ImageAssistant.Configuration.SoftwareSettings类,并且在<SoftwareSettings>节点中我们还看到有<LoadSettings>节点和<PathSettings>节点,其中<LoadSettings>是一个节点集合,还包含有多个子节点,为了表示清楚这些关系我们需要添加四个类:SoftwareSettings、LoadSettingsCollection、LoadSettingsElement及PathSettingElement。为了发布方便,我将这四个类的代码放在一个物理文件中,代码如下(注意添加对System.Configuration.dll的引用):

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Configuration;

namespace ImageAssistant.Configuration

{

public sealed class LoadSettingsCollection : ConfigurationElementCollection

{

private IDictionary<string, bool> settings;

protected override ConfigurationElement CreateNewElement()

{

return new LoadSettingsElement();

}

protected override object GetElementKey(ConfigurationElement element)

{

LoadSettingsElement ep = (LoadSettingsElement)element;

return ep.Key;

}

protected override string ElementName

{

get

{

return base.ElementName;

}

}

public IDictionary<string, bool> Settings

{

get

{

if (settings == null)

{

settings = new Dictionary<string, bool>();

foreach (LoadSettingsElement e in this)

{

settings.Add(e.Key, e.Value);

}

}

return settings;

}

}

public bool this[string key]

{

get

{

bool isLoad = true;

if (settings.TryGetValue(key, out isLoad))

{

return isLoad;

}

else

{

throw new ArgumentException("没有对'" + key + "'节点进行配置。");

}

}

}

}

public class LoadSettingsElement : ConfigurationElement

{

[ConfigurationProperty("key", IsRequired = true)]

public string Key

{

get { return (string)base["key"]; }

set { base["key"] = value; }

}

[ConfigurationProperty("value", IsRequired = true)]

public bool Value

{

get { return (bool)base["value"]; }

set { base["value"] = value; }

}

}

public class PathSettingElement : ConfigurationElement

{

/// <summary>

///

/// </summary>

[ConfigurationProperty("SavePath", IsRequired = true)]

public string SavePath

{

get { return (string)base["SavePath"]; }

set { base["SavePath"] = value; }

}

/// <summary>

///

/// </summary>

[ConfigurationProperty("SearchSubPath", IsRequired = false, DefaultValue = true)]

public bool SearchSubPath

{

get { return (bool)base["SearchSubPath"]; }

set { base["SearchSubPath"] = value; }

}

}

/// <summary>

/// 对应config文件中的

/// </summary>

public sealed class SoftwareSettings : ConfigurationSection

{

/// <summary>

/// 对应SoftwareSettings节点下的LoadSettings子节点

/// </summary>

[ConfigurationProperty("LoadSettings", IsRequired = true)]

public LoadSettingsCollection LoadSettings

{

get { return (LoadSettingsCollection)base["LoadSettings"]; }

}

/// <summary>

/// 对应SoftwareSettings节点下的PathSettings子节点,非必须

/// </summary>

[ConfigurationProperty("PathSettings", IsRequired = false)]

public PathSettingElement PathSetting

{

get { return (PathSettingElement)base["PathSettings"]; }

set { base["PathSettings"] = value; }

}

}

}

在上面的代码中可以看到ConfigurationProperty这个属性,这是表示对应的属性在config文件中的属性名,IsRequired表示是否是必须的属性,还有DefaultValue表示属性的默认值。初次之外,我们还要注意以下关系:SoftwareSettings:根节点,继承自ConfigurationSection。LoadSettingsCollection:子节点集合,继承自ConfigurationElementCollection。LoadSettingsElement:子节点,继承自ConfigurationElement。PathSettingElement:子节点,继承自ConfigurationElement。编写了如下代码之后,我们又该如何使用上面的类呢?其实很简单,如下:

class Program

{

static void Main(string[] args)

{

SoftwareSettings softSettings = ConfigurationManager.GetSection("SoftwareSettings") as SoftwareSettings;

foreach (string key in softSettings.LoadSettings.Settings.Keys)

{

Console.WriteLine("{0}={1}", key, softSettings.LoadSettings[key]);

}

Console.WriteLine("SavePath={0},SearchSubPath={1}", softSettings.PathSetting.SavePath, softSettings.PathSetting.SearchSubPath);

Console.ReadLine();

}

}

这个程序的运行结果如下:LoadBmp=TrueLoadJpg=TrueLoadGif=TrueLoadPng=FalseSavePath=C:/ResizeImages/,SearchSubPath=True

总结:在上面的config文件中通过<appSettings>也达到了类似的效果,但是通过自定义节点我们可以方便地读取相关的应用程序配置,同时也便于维护。如果在开发过程中遇到本文中类似的情况,不妨采取本文所述的方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: