我正在使用 .net 框架 4.5 处理继承的 ASP.NET MVC 4 项目。
我们添加了一个新的配置节文件和相关的类文件,据我们所知(docs.Microsoft 和其他在线指南),它设置正确。
问题
ConfigurationManager.GetSection()
返回空值。
根据文档,如果该部分不存在,则返回 null 。对此进行故障排除很麻烦。
编码
该网站是一个 ASP.NET Web 应用程序。属性窗口将程序集名称设置为 Client.Project.UI.Base(这是已发布 bin 中的 DLL)。这是用于配置类型 FQN 和 web.config 中的程序集的程序集名称。
注意:配置部分 SupportCaseConfiguration 最初位于一个单独的文件中,而 SupportTickets 部分只是指定了 configSource。这已移至 web.config 以减少故障排除时潜在问题的数量。
网络配置:
<configSections>
<!-- define type for new section -->
<section name="SupportTickets" type="Client.Project.UI.Base.Infrastructure.Services.SupportCaseConfigurationSection, Client.Project.UI.Base"/>
</configSections>
<!-- new config section -->
<SupportTickets>
<SupportCaseConfiguration>
<caseTypes>
<add name="tenant.TestCase" label="Test Case" recipient="email_here" ccList="" bccList="" />
</caseTypes>
</SupportCaseConfiguration>
</SupportTickets>
SupportCaseConfiguration.cs:
namespace Client.Project.UI.Base.Infrastructure.Services
{
using System.Configuration;
//Extend the ConfigurationSection class.
public class SupportCaseConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("caseTypes", IsDefaultCollection = true)]
public CaseTypeElementCollection CaseTypes
{
get { return (CaseTypeElementCollection)this["caseTypes"]; }
}
}
//Extend the ConfigurationElementCollection class.
[ConfigurationCollection(typeof(CaseTypeElement))]
public class CaseTypeElementCollection : ConfigurationElementCollection
{
public CaseTypeElement this[int index]
{
get { return (CaseTypeElement)BaseGet(index); }
set
{
if (BaseGet(index) != null)
BaseRemoveAt(index);
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new CaseTypeElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CaseTypeElement)element).Name;
}
}
//Extend the ConfigurationElement class. This class represents a single element in the collection.
public class CaseTypeElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("label", IsRequired = true)]
public string Label
{
get { return (string)this["label"]; }
set { this["label"] = value; }
}
[ConfigurationProperty("recipient", IsRequired = true)]
public string Recipient
{
get { return (string)this["recipient"]; }
set { this["recipient"] = value; }
}
[ConfigurationProperty("ccList", IsRequired = true)]
public string CcList
{
get { return (string)this["ccList"]; }
set { this["ccList"] = value; }
}
[ConfigurationProperty("bccList", IsRequired = true)]
public string BccList
{
get { return (string)this["bccList"]; }
set { this["bccList"] = value; }
}
}
}
在其他地方,获取新的配置数据:
SupportCaseConfigurationSection supportTicketsConfigurationSection = ConfigurationManager.GetSection("SupportCaseConfiguration") as SupportCaseConfigurationSection;
该站点正在本地发布,我可以附加一个调试器以确保使用最新版本的文件。我可以在发布的 web.config 中看到配置部分。
我一直在看这个,我再也看不出有什么不对劲了。对我来说一切都很好...
任何想法、故障排除技巧甚至指出我是布偶都会很有用。
干杯。