简体   繁体   English

自定义配置部分.NET

[英]Custom Config Section .NET

I am trying to create a custom config section for holding some API credentials following the tutorial at: http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx . 我正在尝试按照以下教程创建一个自定义配置部分来保存一些API凭据: http//devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections .aspx I have added the following to my Web.config file: 我已将以下内容添加到我的Web.config文件中:

<!-- Under configSections -->
 <section name="providers" type="EmailApiAccess.Services.ProviderSection, EmailApiAccess.Services"/>

<!-- Root Level -->
<providers>
    <add name="ProviderName" username="" password ="" host="" />
  </providers>

and have created the following classes: 并创建了以下类:

public class ProviderSection: ConfigurationSection
{
    [ConfigurationProperty("providers")]
    public ProviderCollection Providers
    {
        get { return ((ProviderCollection)(base["providers"])); }
    }
}
[ConfigurationCollection(typeof(ProviderElement))]
public class ProviderCollection : ConfigurationElementCollection
{
    public ProviderElement this[int index]
    {
        get { return (ProviderElement) BaseGet(index); }
        set
        {
            if (BaseGet(index) != null)
                BaseRemoveAt(index);

            BaseAdd(index, value);
        }
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ProviderElement)(element)).name;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ProviderElement();
    }
}
public class ProviderElement: ConfigurationElement
{
    public ProviderElement() { }

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

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

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

    [ConfigurationProperty("host", DefaultValue = "",  IsRequired = false)]
    public string host
    {
        get { return (string)this["host"]; }
        set { this["host"] = value; }
    }


}

However, when I try and implement the code using this: 但是,当我尝试使用以下代码实现代码时:

var section = ConfigurationManager.GetSection("providers");
ProviderElement element = section.Providers["ProviderName"];
var creds = new NetworkCredential(element.username, element.password);   

I get an error saying that 'Object' does not contain a definition for 'Providers'.... 我得到一个错误,说“对象”不包含“提供者”的定义....

Any help would be appreciated. 任何帮助,将不胜感激。

ConfigurationManager.GetSection returns a value of type object , not your custom config section. ConfigurationManager.GetSection返回类型为object的值,而不是您的自定义配置节。 Try the following: 请尝试以下方法:

var section = ConfigurationManager.GetSection("providers") as ProviderSection;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM