Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';无法获取我的自定义配置部分_C#_.net_App Config_Configurationmanager - Fatal编程技术网

C# Can';无法获取我的自定义配置部分

C# Can';无法获取我的自定义配置部分,c#,.net,app-config,configurationmanager,C#,.net,App Config,Configurationmanager,我正在尝试创建一个自定义配置节来加载我的应用程序监视器的“烤箱”列表。这是我第一次体验配置部分,我尝试了以下示例;但是,我不知道我错过了什么 当我尝试获取配置部分时,出现以下异常: 创建BurnIn的配置节处理程序时出错:无法从程序集“System.configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“BurnIn.UI.BurnInConfiguration节”。(C:\MKS\Bu

我正在尝试创建一个自定义配置节来加载我的应用程序监视器的“烤箱”列表。这是我第一次体验配置部分,我尝试了以下示例;但是,我不知道我错过了什么

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

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

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

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

BurnInConfigurationSection burnInConfigSection = ConfigurationManager.GetSection(BurnInSection) as BurnInConfigurationSection;
一切似乎都导致了同样的例外

当我查看config.FilePath时,它是“C:\MKS\BurnIn\PC\u 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);
    }
}
}
namespace BurnIn.UI
{
/// 
///app.config中的BurnIn应用程序配置部分
/// 
公共类BurnInConfiguration部分:配置部分
{
[配置属性(“烤箱”,IsDefaultCollection=false)]
[配置收集(类型收集),
AddItemName=“添加”,
ClearItemsName=“清除”,
removitemname=“remove”)]
公共烤炉
{
获取{return(covencollection)base[“covens”];}
设置{base[“covens”]=value;}
}
}
/// 
///烤箱配置信息
/// 
公共类配置:ConfigurationElement
{
公共配置(){}
公共配置(字符串昵称、字符串名称、字符串ip、int slotCount)
{
昵称=昵称;
MesName=MesName;
IP=IP;
SlotCount=SlotCount;
}
[ConfigurationProperty(“昵称”,DefaultValue=“OvenName”,IsRequired=true,IsKey=true)]
公共字符串昵称
{
获取{return(string)this[“昵称”];}
设置{this[“昵称”]=value;}
}
[ConfigurationProperty(“MesName”,DefaultValue=“MesName”,IsRequired=true,IsKey=true)]
公共字符串名称
{
获取{return(string)this[“MesName”];}
设置{this[“MesName”]=value;}
}
[ConfigurationProperty(“IP”,DefaultValue=“10.130.110.20”,IsRequired=true,IsKey=false)]
公共字符串IP
{
获取{return(string)this[“IP”];}
设置{this[“IP”]=value;}
}
[ConfigurationProperty(“SlotCount”,DefaultValue=“20”,IsRequired=true,IsKey=false)]
公共整数时隙计数
{
获取{return(int)this[“SlotCount”];}
设置{this[“SlotCount”]=value;}
}
}
/// 
///收集烤箱配置
/// 
公共类集合:ConfigurationElementCollection
{
公共图书馆馆藏()
{
}
公共资源配置此[int索引]
{
获取{return(OvenConfig)BaseGet(index);}
设置
{
if(BaseGet(index)!=null)
{
BaseRemoveAt(索引);
}
BaseAdd(索引、值);
}
}
公共无效添加(OvenConfig OvenConfig)
{
BaseAdd(配置);
}
公共空间清除()
{
BaseClear();
}
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的配置();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
return((OvenConfig)元素);
}
公共无效删除(OvenConfig OvenConfig)
{
BaseRemove(ovenConfig.昵称);
}
公共无效删除(整数索引)
{
BaseRemoveAt(索引);
}
公共无效删除(字符串名称)
{
BaseRemove(姓名);
}
}
}
这是我的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>

DalConfig.xml
BurnInTestPlanMap.tpx

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

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

该部分用于我正在使用的另一个dll。我试图加载的是:我对你的程序集使用什么?@markshancock然后指定程序集的名称,其中
burninfigurationsection
是declaredIs在主应用程序集(BurnIn.UI)的App.xaml.cs中定义了它@markshancock然后类型名称将是(如果你的程序集没有签名)
BurnIn.UI.BurnInConfigurationSection,BurnIn.UI
只是想添加—因为这让我抓狂—如果在MVC应用程序的web.config中从类型中省略名称空间,它仍然可以工作。但如果在控制台应用程序或服务中不使用名称空间,则不会。算了吧。
type="BurnIn.UI.BurnInConfigurationSection, AssemblyWhereThisTypeIsDeclared"