从web.config读取自定义配置返回null

时间:2014-03-07 20:39:43

标签: c# web-config

我试图从databases / add元素中读出值,但每次都抛出异常。这里有什么不对......我不明白。

如果这是一个线索,这段代码就在它自己的程序集中。

在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>

尝试从该部分获取值。我

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

部分的实施。

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; }
    }
}

将ConfigurationCollection属性添加到类。

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

}

1 个答案:

答案 0 :(得分:0)

这么简单......我只需要在Web配置中将ConfigurationCollectionAttribute更改为我的程序集名称...

在每个样本中,他们引用此属性......

自:     section name =“sessionFactoryConfiguration”type =“Boot.Multitenancy.Configuration.SessionFactoryConfiguration,ConfigurationCollectionAttribute”/&gt;

要:     section name =“sessionFactoryConfiguration”type =“Boot.Multitenancy.Configuration.SessionFactoryConfiguration,Boot.Multitenancy”/&gt;