[英]Reading custom configuration from web.config return null
我试图从数据库/添加元素中读取值,但是每次都会抛出异常。 这里怎么了...我不明白。
如果这是一个线索,则此代码在其自己的程序集中。
在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
}
如此简单...我只需要在Web配置中将ConfigurationCollectionAttribute更改为我的AssemblyName ...
在每个示例中,他们都引用此属性...
来自:节名称=“ sessionFactoryConfiguration” type =“ Boot.Multitenancy.Configuration.SessionFactoryConfiguration,ConfigurationCollectionAttribute” />
若要:部分名称=“ sessionFactoryConfiguration”类型=“ Boot.Multitenancy.Configuration.SessionFactoryConfiguration,Boot.Multitenancy” />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.