Coding with .NET

Live with passion, coding with passion....

首页 新随笔 联系 订阅 管理
  16 Posts :: 3 Stories :: 118 Comments :: 0 Trackbacks

在 .net 1.0/1.1 中,.net framework给我们提供了基于 xml 文件的配置系统,使我们能够非常方便的读取和扩展配置数据。在 .net 2.0 中,这种基于 xml 文件的配置系统被进一步改善了,更容易被读取和扩展,而且增加了在运行时更改配置和加密配置数据的支持。

如何配置应用程序

在 .net 配置系统中一般包括两种类型的元素:

  • Section: 配置节。
  • Section Group: 配置节组,用于将多个配置节(section)或配置节组(section group)组合起来。在 .net 1.0/1.1 中, section group只有一个名称的属性。而在 Whidbey 中,section group也有类型(从 ConfigurationSectionGroup 类继承)。

在配置文件中使用自定义的配置节/配置节组时,需要首先在configSections元素中申明。如下的一个例子(注意 configuration 元素的 xmlns 属性。下面的配置代码仅仅是为了举例,使用了 machine.config 中定义的配置节、配置节组。):

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <!-- Define custom configuration sections/section groups. -->
  <configSections>
    <section name="appSettings" type="type here" restartOnExternalChanges="false" />
    <sectionGroup name="system.web" type="type here">
      <section name="pages" type="type here" />
    </sectionGroup>
  </configSections>
  
  <!-- Configuration data -->
  <appSettings>
    <add key="--" value="---" />
  </appSettings>
  
  <system.web>
    <pages>
      <controls>
        <add tagPrefix="chenglin" namespace="MyNamespace.WebControls" assembly="MyAssembly"/>
      </controls>
    </pages>
  </system.web>
</configuration>

.net framework 配置系统在加载配置数据的时候,会搜索一个由 xml 配置文件构成的树型结构来获取配置数据。在 exe 程序中,一般会搜索两个配置文件:

  • machine.config
  • [app name].exe.config

而在一个 web 应用程序中,可能会包括多个配置文件:

  • machine.config
  • web.config (in app v-root)
  • 进入子目录的web.config,并重复2,直到当前请求页面所在的目录。

如何在应用程序中读取配置数据

在 .net 2.0 中,配置数据可以从 System.Configuration.Configuration 类中获取。要获取配置节和配置节组的数据可以访问 Configuration 类的 GetSection(string sectionName), GetSectionGroup(string sectionGroupName) 等方法。比如上述的例子中,我们可以调用 Configuration.GetSection 方法,参数 sectionName 分别为

  • appSettings
  • system.web/pages
来获取上述例子中两个不同配置节的配置数据;访问 Configuration.GetSectionGroup 方法,参数指定为 system.web,获取配置组的配置数据。

要获取 Configuration 类的实例,可以使用 ConfiguartionManager 或者 WebConfigurationManager 类的相应方法。当然也可以使用 ConfigurationSettings 类的 GetConfig 方法,但是它已经被标记为 Obsolete 。 使用 ConfigurationManager 类或者 WebConfigurationManager 类的另外一个好处是我们指定访问某个配置文件,而不仅仅是默认的配置文件。比如在一个 web 应用程序中,当访问应用程序根目录下的页面的时候,在.net framework 1.0/1.1中我们就不是很容易获取子目录下配置文件的配置内容,但在 2.0 中我们可以指定路径来直接访问。

Configuartion Model 与配置文件的映射

一般来说,我觉得为一个应用程序写的配置部分的代码按如下的步骤比较好:首先,我们要知道应用程序到底需要什么配置;然后用 xml 的形式写出来;最后再根据 xml 代码写出所需的配置代码。当然将后面两步的顺序交换也是不错的选择。

在 .net framework 1.0/1.1 中,配置节的处理程序需要实现 IConfigurationSectionHandler 接口,然后在程序中处理 xml 数据。而在 .net framework 2.0 中则不一定需要实现 IConfigurationSectionHandler 接口,只需从相应的类继承就可以,而且可以省掉繁琐的 xml 处理过程。.net 2.0中的几个主要的类包括:

  • ConfigurationElement,独立的配置元素,在xml配置文件中表示配置节的子节点。
  • ConfigurationElementCollection,表示一组配置元素。
  • ConfigurationSection,在配置文件中表示一个配置节。
  • ConfigurationSectionGroup,表示配置节组,可以包含多个配置节或者配置节组。
比如下面的图表列出了在上述的例子中是如何将不同的元素映射到相应的配置类的:
<appSettings>
  <add key="--" value="---" />
</appSettings>
<system.web>
  <pages>
    <controls>
      <add tagPrefix="chenglin" namespace="MyNamespace.WebControls" assembly="MyAssembly"/>
</controls> </pages> </system.web>
Configuration Section
Configuration Element
   
Configuration Section Group
Configuration Section
Configuration Element Collection
Configuration Element
     

创建配置系统所需的代码

看起来比 .net framework 1.0/1.1 中的复杂吧?实际上非常简单的。下面列出了上述的部分代码(如果需要查看完整的代码,可以用 .Net Reflector 查看):

SystemWebSectionGroup class

1 public sealed class SystemWebSectionGroup : ConfigurationSectionGroup
2 {
3   [ConfigurationProperty("pages")]
4   public PagesSection Pages
5   {
6     getreturn (PagesSection)base.Sections["pages"]; }
7   }
8 }

PagesSection class

 1 public sealed class PagesSection : ConfigurationSection
 2 {
 3   static PagesSection()
 4   {
 5      propControls = new ConfigurationProperty("controls"typeof(TagPrefixCollection), null, ConfigurationPropertyOptions.None);
 6      
 7      properties = new ConfigurationPropertyCollection();
 8      properties.Add( propControls );
 9   }
10   
11   [ConfigurationProperty("controls")]
12   public TagPrefixCollection Controls
13   {
14     get{  return (TagPrefixCollection) base[propControls]; }
15   }
16   
17   protected override ConfigurationPropertyCollection Properties
18   {
19     get{  return PagesSection.properties;  }
20   }
21   
22   private static readonly ConfigurationProperty propControls;
23   private static ConfigurationPropertyCollection properties;
24 }
25 

在 PagesSection 类中最重要的代码在static构造函数中。在此构造函数中,我们需要创建 System.Configuration.ConfigurationProperty 类的实例,在这些实例中定义了配置文件中配置数据的属性。这些 ConfigurationProperty 类的实例被添加到一个 System.Configuration.ConfigurationPropertyCollection 对象中。

.net framework 可以通过一个 protected 的 Properties 属性来获取这些已定义的配置数据的信息,用来将对象序列化成相应的 xml 内容,或者将 xml 配置文件中的配置数据转化成相应的对象。这里比较有意思的是,如果在 ConfigurationProperty 中设置的是 ConfigurationPropertyCollection 的类型,则.net framework会自动将其映射到相应的子元素中。比如在前面的例子配置文件中的 pages/controls 元素。

TagPrefixCollection class

 1 [ConfigurationCollection(typeof(TagPrefixInfo), AddItemName="add")]
 2 public sealed class TagPrefixCollection : ConfigurationElementCollection
 3 {
 4   static TagPrefixCollection()
 5   {
 6     _propControls = new ConfigurationProperty(nulltypeof(TagPrefixCollection), null, ConfigurationPropertyOptions.DefaultCollection);
 7     
 8     _properties = new ConfigurationPropertyCollection();
 9     _properties.Add(TagPrefixCollection._propControls);
10   }
11   
12   protected override ConfigurationElement CreateNewElement()
13   {
14     return new TagPrefixInfo();
15   }
16   
17   protected override object GetElementKey(ConfigurationElement element)
18   {
19     TagPrefixInfo info = (TagPrefixInfo) element;
20     if (info.TagName == null) {
21       return string.Concat( info.TagPrefix, ":", info.Namespace, ":", ((info.Assembly == null? string.Empty : info.Assembly) );
22     }
23     else {
24       return (info.TagPrefix + ":" + info.TagName);
25     }
26   }
27   
28   protected override ConfigurationElementCollectionType CollectionType
29   {
30     get{  return ConfigurationElementCollectionType.BasicMap; }
31   }
32   
33   protected override string ElementName
34   {
35     get{  return "add"; }
36   }
37   
38   protected override ConfigurationPropertyCollection Properties
39   {
40     get{  return TagPrefixCollection._properties; }
41   }
42   
43   private static readonly ConfigurationProperty _propControls;
44   private static ConfigurationPropertyCollection _properties;
45 }
46 

上面的代码和 PagesSection 有很多地方都是相同的。不过需要注意的是,因为这是一个collection,所以允许我们设置集合的属性。有两个地方需要设置:

  • TagPrefixInfoCollection.CollectionType,设置集合的类型,可选项包括 AddRemoveClearMap, AddRemoveClearMapAlternate, BasicMap, BasicMapAlternate。
    • AddRemoveClearMap:
      The default type of ConfigurationElementCollection. Collections of this type contain elements that can be merged across a hierarchy of configuration files. At any particular level within such a hierarchy, add, remove, and clear directives are used to modify any inherited properties and specify new ones.
    • AddRemoveClearMapAlternate:
      Same as AddRemoveClearMap, except that this type causes the ConfigurationElementCollection object to sort its contents such that inherited elements are listed last.
    • BasicMap:
      Collections of this type contain elements that apply to the level at which they are specified, and to all child levels. A child level cannot modify the properties specified by a parent element of this type.
    • BasicMapAlternate:
      Same as BasicMap, except that this type causes the ConfigurationElementCollection object to sort its contents such that inherited elements are listed last.
  • TagPrefixInfoCollection.ElementName 和类的 ConfigurationCollectionAttribute 特性,指定子元素的名称。默认使用 add 。

另外在 TagPrefixInfoCollection 类中很重要的方式有 CreateNewElement 和 GetElementKey,它们被 .net framework 用来创建配置元素的对象,以及被用来确定配置元素在集合中的键值。

TagPrefixInfo class

最后看看 TagPrefixInfo 类。

 1 public sealed class TagPrefixInfo : ConfigurationElement
 2 {
 3   static TagPrefixInfo()
 4   {
 5     s_elemProperty = new ConfigurationElementProperty(new CallbackValidator(typeof(TagPrefixInfo), new ValidatorCallback(Validate)));
 6     _propTagPrefix = new ConfigurationProperty("tagPrefix"typeof(string), "/"null, StdValidatorsAndConverters.NonEmptyStringValidator, ConfigurationPropertyOptions.Required);
 7     _propNamespace = new ConfigurationProperty("namespace"typeof(string), nullnull, StdValidatorsAndConverters.NonEmptyStringValidator, ConfigurationPropertyOptions.None);
 8     _propAssembly = new ConfigurationProperty("assembly"typeof(string), nullnull, StdValidatorsAndConverters.NonEmptyStringValidator, ConfigurationPropertyOptions.None);
 9     
10     _properties = new ConfigurationPropertyCollection();
11     _properties.Add(_propTagPrefix);
12     _properties.Add(_propNamespace);
13     _properties.Add(_propAssembly);
14   }
15   
16   private static void Validate(object value)
17   {
18     // validation code here
19   }
20   
21   [StringValidator(MinLength=1), ConfigurationProperty("assembly")]
22   public string Assembly
23   {
24     get{  return (stringbase[TagPrefixInfo._propAssembly];  }
25     set{  base[TagPrefixInfo._propAssembly] = value;  }
26   }
27   
28   [ConfigurationProperty("namespace"), StringValidator(MinLength=1)]
29   public string Namespace
30   {
31     get{  return (stringbase[TagPrefixInfo._propNamespace]; }
32     set{  base[TagPrefixInfo._propNamespace] = value; }
33   }
34   
35   [StringValidator(MinLength=1), ConfigurationProperty("tagPrefix", RequiredValue=true, DefaultValue="/")]
36   public string TagPrefix
37   {
38     get{  return (stringbase[TagPrefixInfo._propTagPrefix]; }
39     set{  base[TagPrefixInfo._propTagPrefix] = value; }
40   }
41   
42   protected override ConfigurationPropertyCollection Properties
43   {
44     get{  return TagPrefixInfo._properties; }
45   }
46   
47   protected override ConfigurationElementProperty ElementProperty
48   {
49     get{  return TagPrefixInfo.s_elemProperty;  }
50   }
51   
52   private static readonly ConfigurationProperty _propAssembly;
53   private static ConfigurationPropertyCollection _properties;
54   private static readonly ConfigurationProperty _propNamespace;
55   private static readonly ConfigurationProperty _propTagPrefix;
56   private static readonly ConfigurationElementProperty s_elemProperty;
57 }
58 

TagPrefixInfo 类中重载了 ElementProperty 属性,指定如何验证配置数据。因此,对于一个 ConfigurationElement 来说,除了可以使用 ConfigurationProperty 指定元素属性的信息,也可以使用 ConfigurationElementProperty 指定元素的信息,做到配置元素局部和整体的验证。

在创建好上面所以的代码之后,要读取或者创建配置文件都很容易,这里就不介绍了。 :)

posted on 2005-07-27 16:55 Lin 阅读(2495) 评论(7)  编辑 收藏 网摘 所属分类: ASP.Net.Net

Feedback

#1楼 2005-08-27 08:44 zxj[未注册用户]
我的例子如下:(联系我:erpcrm@163.com )

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigTest c = ConfigurationManager.GetSection("testsection") as ConfigTest;
if (c == null)
{
c = new ConfigTest();
c.TestValue1 = "change1";
cfg.Sections.Add("testsection", c);
cfg.Save(); //这儿的保存成功了
}
else
{
c.TestValue1 = "change2";
cfg.Save(); // 在配置文件中 testValue1的值应该改为“change2”,但是运行的结果不是,为什么?????
}


}
}
public class ConfigTest : ConfigurationSection
{
private static ConfigurationProperty prop;
private static ConfigurationPropertyCollection _Properties;
static ConfigTest()
{
_Properties = new ConfigurationPropertyCollection();
prop = new ConfigurationProperty("testValue1", typeof(String), "InitValue");
_Properties.Add(prop);

}

[ConfigurationProperty("testValue1")]
public String TestValue1
{
get
{
return this[prop] as string;
}
set
{
this[prop] = value;
}
}

protected override bool IsModified()
{
return true;
}
public override bool IsReadOnly()
{
return false;
}
}
}

  回复  引用    

#2楼 2005-08-27 08:55 Lin
你需要实现 ConfigurationSection.Properties 属性吧 :)
  回复  引用    

#3楼 2006-01-11 14:45 bryanzk[未注册用户]
你好,我有个关于配置文件的问题,一句两句说不清,能帮我一下吗?
我的email:bryanzk@vip.sina.com
msn:bryanzk@hotmail.com

  回复  引用    

#4楼 2006-03-03 01:47 kmjacky[未注册用户]
你好,我按照你给的例子,自己改了一下,在ASP.net PAGE中修改了CustomSection的default属性, 但save 的时候报错,能帮我看一下这个代码吗?
error:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
我设断点看了一下在
protected override ConfigurationPropertyCollection Properties
{
get{ return MyCustomSecColletions._properties; }
}
一直是死循环,不是太明白,谢谢了。 我的eMAIL:kmjacky@vip.sina.com

以下是web.config的片断:
----------------------------------------------------
<configSections>
<section name="myCustomGroup" restartOnExternalChanges="false" type="ProjSite.Config.MyCustomSectionGrp, App_Code" />
</configSections>
<myCustomGroup configSource="custom.config"></myCustomGroup>
---------------------------------------------------------
custom.config如下:
<myCustomGroup>
<CustomSections>
<CustomSection id="1" corp="C#Babcock" user="TL1\Administrator" default="false" />
<CustomSection id="2" corp="C#Babcock" user="TL1\jacky" default="false" />
<CustomSection id="3" corp="L#Babcock 1 name" user="TL1\Administrator"
default="true" />
<CustomSection id="4" corp="L#Babcock2" user="TL1\Administrator" default="false" />
<CustomSection id="5" corp="L#Babcock3" user="TL1\Administrator" default="false" />
<CustomSection id="6" corp="L#Babcock4" user="TL1\Administrator" default="false" />
<CustomSection id="7" corp="L#Babcock5" user="TL1\Administrator" default="false" />
<CustomSection id="8" corp="L#Babcock 1 name" user="TL1\jacky" default="true" />
<CustomSection id="9" corp="L#Babcock2" user="TL1\jacky" default="false" />
<CustomSection id="10" corp="L#Babcock3" user="TL1\jacky" default="false" />
<CustomSection id="11" corp="L#Babcock4" user="TL1\jacky" default="false" />
<CustomSection id="12" corp="L#Babcock5" user="TL1\jacky" default="false" />
</CustomSections>
</myCustomGroup>

  回复  引用    

#5楼 2006-03-03 01:48 kmjacky[未注册用户]
namespace ProjSite.Config
{
public sealed class MyCustomSectionGrp : ConfigurationSection
{
private Configuration m_cfg;

public void Save()
{
this.SectionInformation.ForceSave = true;
m_cfg.Save(ConfigurationSaveMode.Full);
}
private const string CS_SECTION = "myCustomGroup";
private static MyCustomSectionGrp m_current;
public static MyCustomSectionGrp Current
{
get
{
if (m_current == null)
{
Configuration cfg;
System.Web.HttpContext ctx = System.Web.HttpContext.Current;
if (ctx == null)
cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
else
cfg = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
ctx.Request.ApplicationPath);
m_current = (MyCustomSectionGrp)cfg.Sections[CS_SECTION];
m_current.m_cfg = cfg;
}

return m_current;
}
}

static MyCustomSectionGrp()
{
propCustomSections = new ConfigurationProperty("CustomSections", typeof(MyCustomSecColletions),
null, ConfigurationPropertyOptions.None);
properties = new ConfigurationPropertyCollection();
properties.Add(propCustomSections);
}

[ConfigurationProperty("CustomSections")]
public MyCustomSecColletions customSections
{
get { return (MyCustomSecColletions)base[propCustomSections]; }
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return MyCustomSectionGrp.properties;
}
}

private static readonly ConfigurationProperty propCustomSections;
private static ConfigurationPropertyCollection properties;
}

  回复  引用    

#6楼 2006-03-03 01:49 kmjacky[未注册用户]
[ConfigurationCollection(typeof(MyCustomSec), AddItemName="CustomSection")]
public sealed class MyCustomSecColletions : ConfigurationElementCollection
{
private static readonly ConfigurationProperty _propCustomSections;
private static ConfigurationPropertyCollection _properties;

static MyCustomSecColletions()
{
_propCustomSections = new ConfigurationProperty(null, typeof(MyCustomSecColletions),
null, ConfigurationPropertyOptions.IsDefaultCollection);

_properties = new ConfigurationPropertyCollection();
_properties.Add(MyCustomSecColletions._propCustomSections);
}

protected override ConfigurationElement CreateNewElement()
{
return new MyCustomSec();
}

protected override object GetElementKey(ConfigurationElement element)
{
MyCustomSec cussec = (MyCustomSec)element;
if (cussec.ID == null)
{
return string.Concat(cussec.Corp, ":", cussec.User, ":", ((cussec.Default == null) ? string.Empty : cussec.Default));
}
else
{
return cussec.ID;
}
}

public override ConfigurationElementCollectionType CollectionType
{
get{ return ConfigurationElementCollectionType.BasicMap; }
}

protected override string ElementName
{
get { return "CustomSection"; }
}

public MyCustomSec this[int index]
{
get
{
return (MyCustomSec)BaseGet(index);
}
set
{

if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}

public int IndexOf(MyCustomSec msc)
{
return BaseIndexOf(msc);
}

public void Add(MyCustomSec msc)
{
BaseAdd(msc);
// Add custom code here.
}

protected override void BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false);
// Add custom code here.
}

public void Remove(MyCustomSec msc)
{
if (BaseIndexOf(msc) >= 0)
BaseRemove(msc.ID);
}

public void RemoveAt(int index)
{
BaseRemoveAt(index);
}

public void Remove(string id)
{
BaseRemove(id);
}

public void Clear()
{
BaseClear();
// Add custom code here.
}

protected override ConfigurationPropertyCollection Properties
{
get{ return MyCustomSecColletions._properties; }
}
}

  回复  引用    

#7楼 2006-03-03 01:49 kmjacky[未注册用户]

public sealed class MyCustomSec : ConfigurationElement
{
private static readonly ConfigurationProperty _id;
private static readonly ConfigurationProperty _corp;
private static ConfigurationPropertyCollection _properties;
private static readonly ConfigurationProperty _user;
private static readonly ConfigurationProperty _default;
//private static readonly ConfigurationElementProperty s_elemProperty;

static MyCustomSec()
{
//s_elemProperty = null;
_id = new ConfigurationProperty("id", typeof(string), "");
_corp = new ConfigurationProperty("corp", typeof(string), "");
_user = new ConfigurationProperty("user", typeof(string), "");
_default = new ConfigurationProperty("default", typeof(string), "");

_properties = new ConfigurationPropertyCollection();
_properties.Add(_id);
_properties.Add(_corp);
_properties.Add(_user);
_properties.Add(_default);
}

[ConfigurationProperty("id", DefaultValue = "", IsRequired = true, IsKey = true)]
public string ID
{
get{ return (string)base[MyCustomSec._id]; }
set{ base[MyCustomSec._id] = value; }
}

[ConfigurationProperty("corp", DefaultValue = "", IsRequired = true, IsKey = false)]
[StringValidator(InvalidCharacters = "~!@$%^&*()[]{}/;'\"|\\", MaxLength = 60)]
public string Corp
{
get{ return (string)base[MyCustomSec._corp]; }
set{ base[MyCustomSec._corp] = value; }
}

[ConfigurationProperty("user", DefaultValue = "", IsRequired = true, IsKey = false)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|", MaxLength = 60)]
public string User
{
get{ return (string)base[MyCustomSec._user]; }
set { base[MyCustomSec._user] = value; }
}

[ConfigurationProperty("default", DefaultValue = "false", IsRequired = true, IsKey = false)]
public string Default
{
get{ return (string)base[MyCustomSec._default]; }
set{ base[MyCustomSec._default] = value; }
}
protected override ConfigurationPropertyCollection Properties
{
get{ return MyCustomSec._properties; }
}
//protected override ConfigurationElementProperty ElementProperty
//{
// get{ return MyCustomSec.s_elemProperty; }
//}
}
}

  回复  引用    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 201205




相关文章:

相关链接: