简体   繁体   中英

Reading custom configuration from web.config return null

Im trying to read out values from the databases/add element, but it throws exception each time. Whats wrong here... I dont get it.

This code is in its own assembly if thats a clue.

Configuration in web.config

<sectionGroup name="sessionFactoryConfiguration">
  <section name="Databases" type="Boot.Multitenancy.Configuration.SessionFactoryConfiguration, ConfigurationCollectionAttribute"/>
</sectionGroup>


<sessionFactoryConfiguration>
  <Databases>
        <add name="www.domain.com" autoPersist="true" dbType="SqlCe"/>
        <add name="www.domain.net" autoPersist="true" dbType="SqlCe"/>
</Databases>
</sessionFactoryConfiguration>

Try to get the values from the section. I

var conf = ConfigurationManager.GetSection("Databases") as SessionFactoryConfiguration;
      if (conf == null)
            throw new Exception("Not loaded"); //Throws exception each time.

Implementation of sections.

public class DatabaseSection : ConfigurationElement
{
    public DatabaseSection() { }
    public DatabaseSection(String name, bool autoPersist, DbType dbtype)
    {
        this.Name = name;
        this.AutoPersist = autoPersist;
        this.DbType = dbtype;
    }

    [ConfigurationProperty("name")]
    public String Name
    {
        get { return (String)this["name"]; }
        set { this["name"] = value; }
    }

    [ConfigurationProperty("autoPersist", DefaultValue = false, IsRequired = false)]
    public Boolean AutoPersist
    {
        get { return (Boolean)this["autoPersist"]; }
        set { this["autoPersist"] = value; }
    }

    /// <summary>
    /// DbType
    /// </summary>
    [ConfigurationProperty("dbType", DefaultValue = DbType.SqlCe, IsRequired = false)]
    public DbType DbType
    {
        get { return (DbType)this["dbType"]; }
        set { this["dbType"] = value; }
    }
}

The ConfigurationCollection attribute added to class.

public class SessionFactoryConfiguration : ConfigurationSection
{
    [ConfigurationProperty("Databases", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(DatabaseCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
    public DatabaseCollection Databases
    {
        get { return (DatabaseCollection)base["Databases"]; }
    }
}


public class DatabaseCollection : ConfigurationElementCollection
{
    public DatabaseCollection()
    {
        Add((DatabaseSection)CreateNewElement());
    }

    public override ConfigurationElementCollectionType CollectionType
    {
        get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new DatabaseSection();
    }
 ....//long code

}

So simple... I just needed to change the ConfigurationCollectionAttribute to my assemblyname in web configuration...

In each sample they refer to this attribute...

From: section name="sessionFactoryConfiguration" type="Boot.Multitenancy.Configuration.SessionFactoryConfiguration, ConfigurationCollectionAttribute"/>

To: section name="sessionFactoryConfiguration" type="Boot.Multitenancy.Configuration.SessionFactoryConfiguration, Boot.Multitenancy"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM