C# 从web.config读取自定义配置返回null
我试图从databases/add元素中读取值,但每次它都抛出异常。这里怎么了。。。我不明白 如果这是一个线索的话,这个代码在它自己的程序集中 web.config中的配置C# 从web.config读取自定义配置返回null,c#,web-config,C#,Web Config,我试图从databases/add元素中读取值,但每次它都抛出异常。这里怎么了。。。我不明白 如果这是一个线索的话,这个代码在它自己的程序集中 web.config中的配置 <sectionGroup name="sessionFactoryConfiguration"> <section name="Databases" type="Boot.Multitenancy.Configuration.SessionFactoryConfiguration, Configura
<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>
各节的执行情况
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; }
}
}
这么简单。。。我只需要在web配置中将ConfigurationCollectionAttribute更改为我的assemblyname 在每个示例中,它们都引用此属性 发件人: 节名=sessionFactoryConfiguration类型=Boot.Multitenance.Configuration.sessionFactoryConfiguration,ConfigurationCollectionAttribute/> 致:
section name=sessionFactoryConfiguration type=Boot.Multitenancy.Configuration.sessionFactoryConfiguration,Boot.Multitenancy/>它会引发什么异常?我只是打印出未加载,因为如果有一个值它应该工作。应该有一个异常详细信息。如果我添加:var db=conf.Databases;我得到:对象引用未设置为对象的实例。
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; }
}
}
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
}