这是ConfigurationSection类
using System.Configuration;
namespace CampusWebStore.Config
{
public class PoolerConfig : ConfigurationSection
{
[ConfigurationProperty("PoolId", IsRequired = true)]
public string PoolId { get; set; }
[ConfigurationProperty("Host", IsRequired = true)]
public string Host { get; set; }
[ConfigurationProperty("Port", IsRequired = true)]
public int Port { get; set; }
[ConfigurationProperty("Enabled", IsRequired = true)]
public bool Enabled { get; set; }
}
}
web.config部分定义
<section name="PoolerConfig" type="CampusWebStore.Config.PoolerConfig, CampusWebStore"/>
实际部分
<PoolerConfig
PoolId="asdf-asdf-asdf-asdf"
Host="localhost"
Port="5000"
Enabled="true"
/>
然后加载它的行(在Global.asax.cs中)
PoolerConfig poolerConfig = ConfigurationManager.GetSection("PoolerConfig") as PoolerConfig;
似乎无论我做什么,我的PoolerConfig中的所有属性都是默认值(空字符串,0整数等)。研究表明,这应该很容易,但无济于事,我无法弄明白。
答案 0 :(得分:4)
您不能将get/set
支持者用于配置属性。您必须访问基类才能操作属性。有关示例,请参阅http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx。
变化:
[ConfigurationProperty("PoolId", IsRequired = true)]
public string PoolId { get; set; }
要:
[ConfigurationProperty("PoolId", IsRequired = true)]
public string PoolId
{
get { return (string)this["PoolID"]; }
set { this["PoolID"] = value; }
}