用自定义的节来扩展web.config和machine.config
欢迎进入.NET社区论坛,与300万技术人员互动交流 >>进入
webconfig:
[html]
<?xml version=“1.0”?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,
-->
<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>
获取自定义节点 内容:
[csharp]
//打开配置文件
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对象类
[csharp]
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; }
}
}
【责编:peter】