自定义配置节点(一)

      配置文件在.net应用开发中确实是个比较好的东西。有了它,我们在开发的过程中可以省很多事。这篇文章主要的是讲怎么读取简单的自定义配置节点。

      首先来讲下基本知识。Asp.net中的配置是使用一个XML格式的配置文件来管理的。该文件可以包含应用程序需要的信息,也可以包含自定义的信息。配置文件分为机器级配置文件、应用程序级配置文件、文件夹级配置文件。机器级别的比如:C:WindowsMicrosoft.NETFrameworkv4.0.30319Config 文件夹中的machine.config 和web.config。应用程序级和文件夹级的应该好理解,一般开发项目中用的多。

      ASP.NET使用一种多层配置系统,允许开发人员在不同的级别添加配置信息。在运行时,配置被应用的顺序为:应用默认的machine.config,应用来自计算机的web.config,程序根目录下的web.config,文件夹下的web.config。

     关于web.config中一些节点的作用我这里就不讲了。web.config文件的编程读取。在读取<connenctionStrings>和<appSettings>节点可以用System.Configuration.ConfigurationManger。但如果要修改web.config的时候,就必须用System.Web.Configuration.WebConfigurationManager。WebConfigurationManager 主要成员如下:

   Appsettings属性:提供访问添加到应用程序配置文件<appSettings>配置节点中自定义的信息。

   ConnectionStrings属性:提供访问配置文件中<connectionStrings>节中的数据,可以由名称索引来访问单独的链接字符串。

   OpenWebConfiguration方法:返回指定应用程序中配置信息的Configuration对象。

   OpenMachineConfiguration 方法:返回machine.config中配置信息的Configuration对象。

   尽管使用<appSettings>元素可以存储自定义信息,但是该元素具有限制:该配置节点不能存储结构化的信息。

   假定要将几个相关的设置组合在一起,使应用程序获知如何联系一个远程对象,比如指定一个端口号、服务器位置、URL及用户验证信息。首先创建一个网站,打开根目录下web.config文件,在<configuration>节点中添加

1 <RemotingObject available="true" pollTimeout="00:01:00" location="tcp://OrderComputer:8010/OrderService" ></RemotingObject>
RemotingObject

    要实现这个结构,我们需要定义一个相匹配的类,该类派生自System.Configuration.ConfigurationSection。在网站中添加System.Configuration程序集。然后在App_Code文件夹下添加RemotingObject类。代码如下

 1 using System;
 2 using System.Configuration;
 3 
 4 /// <summary>
 5 /// RemotingObject 的摘要说明
 6 /// </summary>
 7 public class RemotingObject:ConfigurationSection
 8 {
 9 
10     [ConfigurationProperty("available", IsRequired = false, DefaultValue = true)]
11     public bool Available
12     {
13         get { return (bool)base["available"]; }
14         set { base["available"] = value; }
15     }
16 
17     [ConfigurationProperty("pollTimeout", IsRequired = true)]
18     public TimeSpan PollTimeout
19     {
20         get { return (TimeSpan)base["pollTimeout"]; }
21         set
22         {
23             base["pollTimeout"] = value;
24         }
25     }
26 
27     [ConfigurationProperty("location", IsRequired = true)]
28     public string Location
29     {
30         get { return (string)base["location"]; }
31         set { base["location"] = value; }
32     }
33 }
RemotingObject类

  代码中每个属性都使用ConfigurationProperty Attribute 映射到相应的<RemotingObejct>节点的属性名称。如果自定义配置节点添加了一个属性,但是没有包含匹配的ConfigurationProperty特性的话,那么运行时有抛出一个异常。

   创建完后,在web.config配置文件中的<configSections>区块中添加一个<section>,代码如下

1   <section name="RemotingObject" type="RemotingObject"/>
section

   最后我们就可以使用代码来从自定义的配置节点中获取信息了。代码如下

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Configuration;
 8 using System.Web.Configuration;
 9 
10 public partial class _Default : Page
11 {
12     protected void Page_Load(object sender, EventArgs e)
13     {
14         System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration("~/");
15         RemotingObject custSection = (RemotingObject)config.GetSection("RemotingObject");
16         Label1.Text += "获取自定义配置节点的信息...<br/>" +
17             "<b>位置</b>" + custSection.Location +
18             "<br/><b>是否可用:</b>" + custSection.Available.ToString() +
19             "<br/> <b>超时时间:</b>" + custSection.PollTimeout.ToString() + "<br/><br/>";
20     }
21 }
Test

 注意加命名空间:using System.Web.Configuration;运行效果如下

 总结:要扩展一个配置文件需要三个步骤

  1.决定想要在配置文件中存储的信息及如何将这些信息组织为元素和属性。

  2.对于每一个新元素,创建一个C#类来封装信息。

  3.向配置文件中注册新的配置块,需要使用<configSection>元素。

   

原文地址:https://www.cnblogs.com/7579/p/3244908.html