简体   繁体   English

ASP.NET MVC 自定义配置 GetSection 返回 null

[英]ASP.NET MVC Custom configuration GetSection returns null

I'm working on an inherited ASP.NET MVC 4 project using .net framework 4.5.我正在使用 .net 框架 4.5 处理继承的 ASP.NET MVC 4 项目。

We've added a new configuration section files and relevant class files and from what we can tell (docs.Microsoft and other online guides) it's set up correctly.我们添加了一个新的配置节文件和相关的 class 文件,据我们所知(docs.Microsoft 和其他在线指南),它设置正确。

The Problem问题

ConfigurationManager.GetSection() returns null. ConfigurationManager.GetSection()返回 null。

According to the docs this returns null if the section doesn't exist.根据文档,如果该部分不存在,则返回 null 。 Troubleshooting this has been troublesome.对此进行故障排除很麻烦。

The Code编码

The website is an ASP.NET Web Application.该网站是一个 ASP.NET Web 应用程序。 Properties window sets assembly name to Client.Project.UI.Base (which is the DLL in the published bin).属性 window 将程序集名称设置为 Client.Project.UI.Base(即已发布 bin 中的 DLL)。 This is the assembly name used for the config types FQN and assembly in web.config.这是用于配置类型 FQN 和 web.config 中的程序集的程序集名称。

NB: the config section SupportCaseConfiguration was originally in a separate file and the SupportTickets section just specified the configSource.注意:配置部分 SupportCaseConfiguration 最初位于一个单独的文件中,而 SupportTickets 部分只是指定了 configSource。 This has been moved into the web.config to reduce the number of potential issues while troubleshooting.这已移至 web.config 以减少故障排除时潜在问题的数量。

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: 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; }
        }
    }
}

Elsewhere, getting new config data:在其他地方,获取新的配置数据:

SupportCaseConfigurationSection supportTicketsConfigurationSection = ConfigurationManager.GetSection("SupportCaseConfiguration") as SupportCaseConfigurationSection;

The site is being published locally, I can attach a debugger to ensure the latest versions of files are being used.该站点正在本地发布,我可以附加一个调试器以确保使用最新版本的文件。 I can see the config section in the published web.config.我可以在已发布的 web.config 中看到配置部分。

I've been looking at this I can no longer see if anything is amiss.我一直在看这个,我再也看不出有什么不对劲了。 It all looks fine for me...对我来说一切都很好...

Any ideas, troubleshooting tips or even pointing out I'm being a muppet would be useful.任何想法、故障排除技巧,甚至指出我是布偶都会很有用。

Cheers.干杯。

I can only assume the issue was something to do with the config classes in the site assembly.我只能假设问题与站点程序集中的配置类有关。

Even after trying online examples, copypasting into the project, not even they worked.即使在尝试了在线示例,复制粘贴到项目中之后,它们甚至都不起作用。

As soon as I put the configuration section/element classes in a separate project (moved out of the website project) it started working.一旦我将配置部分/元素类放在一个单独的项目中(从网站项目中移出),它就开始工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM