首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >配置部分未加载

配置部分未加载
EN

Stack Overflow用户
提问于 2015-11-04 17:47:38
回答 1查看 43关注 0票数 0

我已经在web.config (在configSection元素中)中声明了配置部分:

代码语言:javascript
复制
<section name="gmailEmail" type="MyApp.Communication.Sections.GmailSection" allowLocation="true" allowDefinition="Everywhere" />

和用法:

代码语言:javascript
复制
<gmailEmail>
  <from emailAddress="pat.wasiewicz@gmail.com" name="MyApp"></from>
  <server password="mysuperpass">
</gmailEmail>

我的配置模型:

代码语言:javascript
复制
public class GmailSection : ConfigurationSection
{
    [ConfigurationProperty("server")]
    public ServerElement Server { get; set; }

    [ConfigurationProperty("from")]
    public FromElement From { get; set; }
}

public class ServerElement : ConfigurationElement
{


    [ConfigurationProperty("host", DefaultValue = "smtp.gmail.com", IsRequired = false)]
    public string Host { get; set; }

    [ConfigurationProperty("port", DefaultValue = 587, IsRequired = false)]
    public int Port { get; set; }

    [ConfigurationProperty("ssl", DefaultValue = true, IsRequired = false)]
    public bool Ssl { get; set; }

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

public class FromElement : ConfigurationElement
{
    [ConfigurationProperty("emailAddress", IsRequired = true)]
    public string EmailAddress { get; set; }

    [ConfigurationProperty("name", IsRequired = false)]
    public string Name { get; set; }
}

不出所料,有一个问题:

代码语言:javascript
复制
var configSection = (GmailSection)ConfigurationManager.GetSection("gmailEmail");

configSection.ServernullconfigSection.Fromnull。为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-04 20:23:31

配置部分的类型名称缺少程序集名称。

代码语言:javascript
复制
<section name="gmailEmail" type="MyApp.Communication.Sections.GmailSection, MyApp" allowLocation=.....

服务器/密码元素缺少关闭标记( /> )

代码语言:javascript
复制
<server password="mysuperpass" />

但我觉得这是排字。

应该更改所有的{ get;set;}属性实现,以使用相同的键调用基类的索引器。我已经实现了GmailSection类,您可以以同样的方式修改ServerElement和FormElement类。

代码语言:javascript
复制
public class GmailSection : ConfigurationSection
{
    [ConfigurationProperty("server")]
    public ServerElement Server
    {
        get
        {
            return (ServerElement)this["server"];
        }
        set
        {
            this["server"] = value;
        }
    }

    [ConfigurationProperty("from")]
    public FromElement From
    {
        get
        {
            return (FromElement)this["from"];
        }
        set
        {
            this["from"] = value;
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33528640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档