[英]Config section not loading
I've declaration of my config section in web.config (in configSection
element): 我在web.config中声明了config部分(在
configSection
元素中):
<section name="gmailEmail" type="MyApp.Communication.Sections.GmailSection" allowLocation="true" allowDefinition="Everywhere" />
And usage: 和用法:
<gmailEmail>
<from emailAddress="pat.wasiewicz@gmail.com" name="MyApp"></from>
<server password="mysuperpass">
</gmailEmail>
My config model: 我的配置模型:
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; }
}
Unfortanely, there is a problem: 不幸的是,有一个问题:
var configSection = (GmailSection)ConfigurationManager.GetSection("gmailEmail");
configSection.Server
is null
and configSection.From
is null
. configSection.Server
为null
而configSection.From
为null
。 Why? 为什么?
The type name for the configuration section is missing the assembly name 配置节的类型名称缺少程序集名称
<section name="gmailEmail" type="MyApp.Communication.Sections.GmailSection, MyApp" allowLocation=.....
The server/password element is missing the close tag ( /> ) 服务器/密码元素缺少关闭标签()
<server password="mysuperpass" />
But I think these are typos. 但是我认为这些都是错别字。
You should change all your { get; 您应该更改所有{ set;
组; } property implementations to call the base class' indexer with the same key.
}属性实现,以使用相同的键调用基类的索引器。 I have implemented the GmailSection class, and you can modify the ServerElement and FormElement classes the same way.
我已经实现了GmailSection类,您可以用相同的方式修改ServerElement和FormElement类。
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;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.