Ich habe einen Config-Abschnitt in meinem app.config in folgenden Weise definiert:Konfigurationssystem fehlgeschlagen Fehler zu initialisieren, während eines benutzerdefinierten Abschnitts von Laden app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="RegisterCompanies"
type="ConfigTest.RegisterCompaniesConfig, ConfigTest"
allowLocation="true"
allowDefinition="Everywhere"/>
</configSections>
<RegisterCompanies>
<Companies>
<Company name="Tata Motors" code="Tata"/>
<Company name="Honda Motors" code="Honda"/>
</Companies>
</RegisterCompanies>
</configuration>
diese Informationen zu lesen i drei Klassen erstellt habe in einer solchen Art und Weise: RegisterCompaniesConfig Klasse
public class RegisterCompaniesConfig : ConfigurationSection
{
public static RegisterCompaniesConfig GetConfig()
{
string path = Path.Combine(Application.StartupPath, "ConfigTest.exe.config");
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(path);
RegisterCompaniesConfig serviceSection = ConfigurationManager.GetSection("RegisterCompanies") as RegisterCompaniesConfig;
return serviceSection;
//return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies");
}
[System.Configuration.ConfigurationProperty("Companies")]
public Companies Companies
{
get
{
object o = this["Companies"]; return o as Companies;
}
}
}
dann Firmen Klasse:
public class Companies : ConfigurationElementCollection
{
[System.Configuration.ConfigurationProperty("Company")]
public Company this[int index]
{
get
{
return base.BaseGet(index) as Company;
}
set
{
if (base.BaseGet(index) != null)
{ base.BaseRemoveAt(index); } this.BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Company();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Company)element).Name;
}
}
und die letzte ist Firmenklasse:
public class Company : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return this["name"] as string;
}
}
[ConfigurationProperty("code", IsRequired = true)]
public string Code
{
get
{
return this["code"] as string;
}
}
}
danach, wenn ich durch den Aufruf der Sektion Acess wollen Methode folgende
var config = RegisterCompaniesConfig.GetConfig();
erhalte ich die Fehlermeldung: Konfigurationssystem konnte nicht initialisiert werden Bitte jemand hat einen Blick auf diesen oben genannten Code, wo ist das Problem, es sieht alles ist in Ordnung für mich ....
Das Problem muss in der Datei app.config sein, können Sie es vollständig anzeigen? – RRUZ
Ich habe die komplette app.config-Datei hinzugefügt, bitte sehen Sie sie sich an ... –
Können Sie einen Stack Trace für den Fehler anzeigen, den Sie erhalten? – Rob