当前位置: 移动技术网 > IT编程>开发语言>.net > 用自定义的节来扩展web.config和machine.config配置文件的结构

用自定义的节来扩展web.config和machine.config配置文件的结构

2018年09月20日  | 移动技术网IT编程  | 我要评论

神秘爹地妈咪爱出逃,郝明莉近况,成都摄影论坛

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; } 
    } 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网