Я работаю над унаследованным проектом ASP. NET MVC 4, используя. net framework 4.5.
Мы добавили новые файлы раздела конфигурации и соответствующие файлы классов и из того, что мы можем сказать (документы Microsoft и другие онлайн-руководства), он настроен правильно.
Проблема
ConfigurationManager.GetSection()
возвращает ноль.
Согласно документации это возвращает нуль, если раздел не существует. Поиск и устранение неисправностей оказался проблематичным.
Код
Веб-сайт представляет собой веб-приложение ASP. NET. Окно свойств устанавливает имя сборки в Client.Project.UI.Base (которая является DLL в опубликованном бункере). Это имя сборки, используемое для типов конфигурации FQN и сборки в web.config.
Примечание: раздел конфигурации 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.
Я смотрел на это, я больше не вижу, если что-то не так. Все это выглядит хорошо для меня ...
Любые идеи, советы по устранению неполадок или даже указание на то, что я маппет, были бы полезны.
Приветствия.