简体   繁体   English

ConfigurationManager.GetSection始终为对象提供默认值

[英]ConfigurationManager.GetSection always gives object with default values

Here is the ConfigurationSection class 这是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; }
    }
}

The web.config section definition web.config节定义

<section name="PoolerConfig" type="CampusWebStore.Config.PoolerConfig, CampusWebStore"/>

The actual section 实际部分

<PoolerConfig
    PoolId="asdf-asdf-asdf-asdf"
    Host="localhost"
    Port="5000"
    Enabled="true"
  />

And then the line that loads it (in Global.asax.cs) 然后是加载它的行(在Global.asax.cs中)

PoolerConfig poolerConfig = ConfigurationManager.GetSection("PoolerConfig") as PoolerConfig;

It seems that no matter what I do, all the properties in my PoolerConfig are default values (null strings, 0 ints, etc). 看来,不管我做什么,PoolerConfig中的所有属性都是默认值(空字符串,0个整数等)。 Research indicates this should be easy as pie, but to no avail I cannot figure this out. 研究表明,这应该很容易做到,但无济于事,我无法弄清楚。

You cannot use get/set backers for configuration properties. 您不能将get/set支持者用于配置属性。 You must access the base class to manipulate the properties. 您必须访问基类以操纵属性。 See http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx for an example. 有关示例,请参见http://msdn.microsoft.com/zh-cn/library/2tw134k3(v=vs.100).aspx

Change: 更改:

 [ConfigurationProperty("PoolId", IsRequired = true)]
 public string PoolId { get; set; }

To: 至:

 [ConfigurationProperty("PoolId", IsRequired = true)]
 public string PoolId 
 {
    get { return (string)this["PoolID"]; }
    set { this["PoolID"] = value; }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ConfigurationSection ConfigurationManager.GetSection()始终返回null - ConfigurationSection ConfigurationManager.GetSection() always returns null ConfigurationManager.GetSection()函数 - ConfigurationManager.GetSection() function ConfigurationManager.GetSection给出错误“无法从程序集加载类型...” - ConfigurationManager.GetSection Gives Error “Could not load type…from assembly…” ConfigurationManager.GetSection跳过重复项 - ConfigurationManager.GetSection Skips Duplicates ConfigurationManager.GetSection 返回 null - ConfigurationManager.GetSection returns null ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取) - ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection) ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? ConfigurationManager.GetSection不返回自定义部分 - ConfigurationManager.GetSection doesn't return custom section 如何在.Net Core 2.2中正确使用ConfigurationManager.GetSection() - How to use properly ConfigurationManager.GetSection() in .Net Core 2.2 为什么ConfigurationManager.GetSection“ system.webServer / handlers”不可用? - Why is the ConfigurationManager.GetSection “system.webServer/handlers” not available?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM