用自定义的节来扩展web.config和machine.config配置文件的结构
webconfig:
<?xml version="1.0"?><!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --><configuration><configSections><section name="RemotingObject" type="RemotingObject"/></configSections><system.web><compilation debug="true" targetFramework="4.0"/></system.web><RemotingObject available="true" pollTimeout="00:01:00" location="tcp://OrderComputer:8010/OrderService"/></configuration>
获取自定义节点 内容:
//打开配置文件 Configuration config = WebConfigurationManager.OpenWebConfiguration("~/"); //获取配置节对象 RemotingObject custSection = (RemotingObject)config.GetSection("RemotingObject"); //显示配置节信息 lblInfo.Text += "获取自定义配置节的信息...<br />" + "<b>位置:</b> " + custSection.Location + "<br /><b>是否可用:</b> " + custSection.Available.ToString() + "<br /><b>超时时间:</b> " + custSection.PollTimeout.ToString() + "<br /><br />";
RemotingObject对象类
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Configuration;/// <summary>///RemotingObject 的摘要说明/// </summary>public class RemotingObject : ConfigurationSection{ //定义远程对象是否可用 [ConfigurationProperty("available", IsRequired = false, DefaultValue = true)] public bool Available { get { return (bool)base["available"]; } set { base["available"] = value; } } //定义远程对象的超时时间 [ConfigurationProperty("pollTimeout", IsRequired = true)] public TimeSpan PollTimeout { get { return (TimeSpan)base["pollTimeout"]; } set { base["pollTimeout"] = value; } } //定义远程对象所在的位置 [ConfigurationProperty("location", IsRequired = true)] public string Location { get { return (string)base["location"]; } set { base["location"] = value; } }}