我正在使用.net framework 4.5做一个继承的ASP.NET MVC 4项目。
我们已经添加了一个新的配置部分文件和相关的类文件,并且从我们所能知道的(docs.Microsoft和其他在线指南)来看,它的设置是正确的。
问题
ConfigurationManager.GetSection()
返回null。
根据文档,如果该部分不存在,会返回null。解决这个问题很麻烦。
代码
该网站是一个ASP.NET Web应用程序。属性窗口将汇编名称设置为Client.Project.UI.Base(就是发布仓中的DLL)。这是web.config中的config类型FQN和assembly使用的汇编名。
NB:配置部分SupportCaseConfiguration原本是在一个单独的文件中,SupportTickets部分只是指定了configSource。现在已经移到web.config中,以减少故障排除时的潜在问题。
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中的config部分。
我一直在看这个我已经看不出有什么问题了。对我来说,这一切看起来都很好...
任何想法,排除故障的技巧,甚至指出我是一个傀儡将是有用的。
干杯。
我只能认为这个问题是和网站汇编中的配置类有关。
即使尝试了网上的例子,复制到项目中,也没有成功。
只要我把配置节元素类放在一个单独的项目中(从网站项目中移出),就开始工作了。