首页 新闻 会员 周边 捐助

app.config 自定义配置节 配置系统未能初始化

0
悬赏园豆:20 [已关闭问题] 关闭于 2017-07-14 08:08
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="BaseCon">
      <section name="Base" type="System.Configuration.NameValueSectionHandler" />
    </sectionGroup>
    <sectionGroup name="DSVS">
      <section name="DSVS1" type="System.Configuration.NameValueSectionHandler" />
      <section name="DSVS2" type="System.Configuration.NameValueSectionHandler" />
    </sectionGroup>
    <sectionGroup name ="UAMS">
      <section name="UAMS1" type="System.Configuration.NameValueSectionHandler"/>
      <section name="UAMS2" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
    <sectionGroup name="ESign">
      <section name="Esign1" type="System.Configuration.NameValueSectionHandler"/>
      <section name="Esign2" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
    <sectionGroup name="TSS">
      <section name="TSS1" type="System.Configuration.NameValueSectionHandler"/>
      <section name="TSS2" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
    <sectionGroup name="PDFSign">
      <section name="PDFSign1" type="System.Configuration.NameValueSectionHandler"/>
      <section name="PDFSign2" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
  </configSections>
  <BaseCon>
    <Base>
      <add key="P_path" value="C://bjcadr_log.txt"/>
      <add key="Sent_url" value="liubei@bjca.org.cn"/>
      <add key="Sent_Subject" value="某某公司检测结果"/>
    </Base>
  </BaseCon>
  <DSVS>
    <DSVS1>
      <add key="P_IP_Port" value = "192.168.10.123:8081"/>
      <add key="P_Admin_Port" value="192.168.10.123:80"/>
      <add key="P_interface" value="Sign()"/>
    </DSVS1>
    <DSVS2>
      <add key="P_IP_Port" value = "192.168.10.123:8082"/>
      <add key="P_Admin_Port" value="192.168.10.123:80"/>
      <add key="P_interface" value="Sign()"/>
    </DSVS2>
  </DSVS>
  <UAMS>
    <UAMS1>
      <add key="PIP_Port" value="172.10.128.22:80"/>
      <add key="P_interface_IP_Port" value = "172.10.128.22:80/uumsinterface"/>
      <add key="P_SSO_IP_Port" value="172.10.128.22:80/SSOService"/>
      <add key="P_Def_IP_Port" value="172.10.128.23:9001"/>
    </UAMS1>
    <UAMS2>
      <add key="PIP_Port" value="172.10.128.22:80"/>
      <add key="P_interface_IP_Port" value = "172.10.128.22:80/uumsinterface"/>
      <add key="P_SSO_IP_Port" value="172.10.128.22:80/SSOService"/>
      <add key="P_Def_IP_Port" value="172.10.128.23:9001"/>
    </UAMS2>
  </UAMS>
  <ESign>
    <ESign1>
      <add key="P_IP" value="172.10.128.22:80"/>
      <add key="P_interface" value="Sign()"/>
    </ESign1>
    <ESign2>
      <add key="P_IP" value="172.10.128.22:82"/>
      <add key="P_interface" value="Sign()"/>
    </ESign2>
  </ESign>
  <TSS>
    <TSS1>
      <add key="P_IP_Port" value="172.10.128.22:8002"/>
      <add key="P_Admin_Port" value="172.10.128.22:80"/>
      <add key="P_interface" value="Sign()"/>
    </TSS1>
    <TSS2>
      <add key="P_IP_Port" value="172.10.128.22:8004"/>
      <add key="P_Admin_Port" value="172.10.128.22:80"/>
      <add key="P_interface" value="Sign()"/>
    </TSS2>
  </TSS>
  <PdfSign>
    <PDFSign1>
      <add key="P_IP_Port" value="172.10.128.22:8002"/>
      <add key="P_Admin_Port" value="172.10.128.22:80"/>
      <add key="P_interface" value="Sign()"/>
    </PDFSign1>
    <PDFSign2>
      <add key="P_IP_Port" value="172.10.128.22:8002"/>
      <add key="P_Admin_Port" value="172.10.128.22:80"/>
      <add key="P_interface" value="Sign()"/>
    </PDFSign2>
  </PdfSign>
</configuration>


请问,为什么原本没有问题,后来添加了几个节点后就开始一直报错了呢?

问题补充:

public void LoadDSVSSection()
        {
            //读取DSVS SectionGroup
            int i = 1;
            ConfigurationSectionGroup sample = (ConfigurationSectionGroup)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).SectionGroups["DSVS"];
            foreach (ConfigurationSection var in sample.Sections)
            {
                SectionInformation si = var.SectionInformation;
                NameValueCollection Path = (NameValueCollection)ConfigurationManager.GetSection("DSVS/" + si.Name);
                //每读取一个则新增一个button
                string DSIP = Path["P_IP_Port"].ToString();
                Button btn = new Button();
                btn.Size = new Size(159, 35);
                btn.Location = new Point(6, i * 6 + (i - 1) * 35);
                btn.FlatStyle = FlatStyle.Popup;
                btn.Text = si.Name + "\r\n(" + DSIP + ")";
                btn.UseVisualStyleBackColor = true;
                btn.Click += new EventHandler(this.btnDSVS_Click_1);
                foreach (Control con in tcPro.Controls)
                {
                    if (con.Text == "DSVS")
                    {
                        con.Controls.Add(btn);
                    }
                }
                i++;
            }
        }

seaconch的主页 seaconch | 老鸟四级 | 园豆:4823
提问于:2013-08-15 13:18
< >
分享
所有回答(1)
2

1、<configSections> 必须是 <configuration> 第一个元素。

2、检查你的代码在读取配置时,是否有 BUG;

Launcher | 园豆:45050 (高人七级) | 2013-08-15 13:24

NameValueCollection Path = (NameValueCollection)ConfigurationManager.GetSection("DSVS/" + si.Name);

 每次读取到这里就出错了,DSVS/si.Name=DSVS/DSVS1

支持(0) 反对(0) seaconch | 园豆:4823 (老鸟四级) | 2013-08-15 13:33

@Epic_Price: ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).

返回一个 Configuration 对象,你这样试试:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.GetSection("DSVS/" + si.Name);

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2013-08-15 13:45

@Launcher: 您好,Config.GetSection("DSVS/DSVS1")结果为ConfigurationSection 对象

上文Foreach其实遍历出来的就是ConfigurationSection对象。

但是没有找到获取Value的方法,所以才用的NameValueCollection...

请问,如何从ConfigurationSection对象获得其下<add ....>中values的值...

支持(0) 反对(0) seaconch | 园豆:4823 (老鸟四级) | 2013-08-15 14:04

@Launcher: 方法本身应该没有错的。原本在只有DSVS 和BaseCon节点时是可以成功生成Button的..

支持(0) 反对(0) seaconch | 园豆:4823 (老鸟四级) | 2013-08-15 14:07

@Epic_Price: ConfigurationSection Item ,Properties 

不过话说回来,我使用的时候,都是定义了强类型:

public class xxxxxxConfig :  ConfigurationSection{};

public class xxxxxxxElement : ConfigurationElement{};

 

http://msdn.microsoft.com/zh-cn/library/2tw134k3(v=vs.80).aspx

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2013-08-15 14:18

@Launcher: 嗯,嗯。您说的在理。谢谢.  我找到错误了。 是应为ESign和Esign  的S大小写 错了...

支持(0) 反对(0) seaconch | 园豆:4823 (老鸟四级) | 2013-08-15 14:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册