无法获取我的自定义配置部分

Posted

技术标签:

【中文标题】无法获取我的自定义配置部分【英文标题】:Can't get my custom configuration section 【发布时间】:2012-10-20 03:25:30 【问题描述】:

我正在尝试创建一个自定义配置部分来加载我的应用程序监视器的“烤箱”列表。这是我对配置部分的第一次体验,我尝试按照示例进行操作;但是,我无法弄清楚我错过了什么。

当我尝试获取配置部分时,出现以下异常:

为 BurnIn 创建配置节处理程序时出错:无法从程序集“System.Configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“BurnIn.UI.BurnInConfigurationSection”。 (C:\MKS\BurnIn\PC_SW\bin\BurnIn.UI.vshost.exe.config 第 8 行)

我主要尝试过: System.Configuration.Configuration 配置 = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (config.Sections[BurnInSection] == null) ...

BurnInConfigurationSection burnInConfigSection = config.GetSection(BurnInSection) as BurnInConfigurationSection; 

BurnInConfigurationSection burnInConfigSection = ConfigurationManager.GetSection(BurnInSection) as BurnInConfigurationSection;

一切似乎都导致相同的异常

当我查看 config.FilePath 时,它是“C:\MKS\BurnIn\PC_SW\bin\BurnIn.UI.vshost.exe.config”,我已验证它与 app.config 文件匹配。

这是我的配置类:

namespace BurnIn.UI

/// <summary>
/// BurnIn Application configuration section in app.config
/// </summary>
public class BurnInConfigurationSection : ConfigurationSection

    [ConfigurationProperty("Ovens", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(OvenCollection),
        AddItemName = "add",
        ClearItemsName = "clear",
        RemoveItemName = "remove")]
    public OvenCollection Ovens
    
        get  return (OvenCollection)base["Ovens"]; 
        set  base["Ovens"] = value; 
    


/// <summary>
/// Oven configuration information
/// </summary>
public class OvenConfig : ConfigurationElement

    public OvenConfig()  

    public OvenConfig(string nickName, string mesName, string ip, int slotCount)
    
        NickName = nickName;
        MesName = mesName;
        IP = ip;
        SlotCount = slotCount;
    

    [ConfigurationProperty("NickName", DefaultValue = "OvenName", IsRequired = true, IsKey = true)]
    public string NickName
    
        get  return (string)this["NickName"]; 
        set  this["NickName"] = value; 
    

    [ConfigurationProperty("MesName", DefaultValue = "MesName", IsRequired = true, IsKey = true)]
    public string MesName
    
        get  return (string)this["MesName"]; 
        set  this["MesName"] = value; 
    

    [ConfigurationProperty("IP", DefaultValue = "10.130.110.20", IsRequired = true, IsKey = false)]
    public string IP
    
        get  return (string)this["IP"]; 
        set  this["IP"] = value; 
    

    [ConfigurationProperty("SlotCount", DefaultValue = "20", IsRequired = true, IsKey = false)]
    public int SlotCount
    
        get  return (int)this["SlotCount"]; 
        set  this["SlotCount"] = value; 
    


/// <summary>
/// Collection of Oven Configs
/// </summary>
public class OvenCollection : ConfigurationElementCollection

    public OvenCollection()
    
    

    public OvenConfig this[int index]
    
        get  return (OvenConfig)BaseGet(index); 
        set
        
            if (BaseGet(index) != null)
            
                BaseRemoveAt(index);
            
            BaseAdd(index, value);
        
    

    public void Add(OvenConfig ovenConfig)
    
        BaseAdd(ovenConfig);
    

    public void Clear()
    
        BaseClear();
    

    protected override ConfigurationElement CreateNewElement()
    
        return new OvenConfig();
    

    protected override object GetElementKey(ConfigurationElement element)
    
        return ((OvenConfig)element).NickName;
    

    public void Remove(OvenConfig ovenConfig)
    
        BaseRemove(ovenConfig.NickName);
    

    public void RemoveAt(int index)
    
        BaseRemoveAt(index);
    

    public void Remove(string name)
    
        BaseRemove(name);
    


这是我的 app.config:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Biotronik.NGMP.DAL.Sources.DalBaseSettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    <section name="BurnIn" type="BurnIn.UI.BurnInConfigurationSection"/>
  </configSections>
  <applicationSettings>
    <Biotronik.NGMP.DAL.Sources.DalBaseSettings>
      <setting name="ConfigFileName" serializeAs="String">
        <value>DalConfig.xml</value>
      </setting>
      <setting name="MappingFileName" serializeAs="String">
        <value>BurnInTestPlanMap.tpx</value>
      </setting>
    </Biotronik.NGMP.DAL.Sources.DalBaseSettings>
  </applicationSettings>
  <connectionStrings>
    <add name="BurnInConnection" connectionString="metadata=res://*/BurnIn.csdl|res://*/BurnIn.ssdl|res://*/BurnIn.msl;provider=Oracle.DataAccess.Client;provider connection string=&quot;DATA SOURCE=XXXX;PASSWORD=xxxx;PERSIST SECURITY INFO=True;USER ID=XXXX&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <log4net configSource="BurnInLog4net.config"/>
  <BurnIn>
    <Ovens>
      <add NickName="Mark's Oven" MesName="MESBOven" IP="10.130.110.20" SlotCount="5"/>
      <add NickName="Real Oven" MesName="MESOven1" IP="10.130.110.50" SlotCount="20"/>
    </Ovens>
    </BurnIn>
  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>

【问题讨论】:

【参考方案1】:

您在这里输入了错误的配置节类型名称:

<section name="Biotronik.NGMP.DAL.Sources.DalBaseSettings"  
    type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0,  
    Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

您应该在此处使用自定义配置部分类型的名称:

type="BurnIn.UI.BurnInConfigurationSection, AssemblyWhereThisTypeIsDeclared"

【讨论】:

该部分适用于我正在使用的不同 dll。我要加载的是:

以上是关于无法获取我的自定义配置部分的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的自定义 XML RPC 获取配置不起作用?

无法从android中的自定义列表视图中获取所选项目

无法从 Spring Boot 应用程序中的自定义 yml 文件加载配置

如何在 .NET 中为自定义配置部分启用 configSource 属性?

如何在 app.config 中为自定义部分获取智能感知?

无法使用 .xib 文件在我的自定义单元格中添加任何内容?