Search code examples
asp.netweb-configcustom-configuration

ConfigurationManager.GetSection returning null for appparently correct 'path'


This is regarding a web.config file

Here's the ConfigSection

<configSections>
<sectionGroup name="HttpExceptionHandler">
  <section name="errorLog" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <section name="errorMail" type="System.Configuration.SingleTagSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>

Here's the SectionGroup:

<HttpExceptionHandler>
    <errorLog type="MI.Generic.HttpExceptionHandler.SqlErrorLog, MI.Generic.HttpExceptionHandler" dataSource="opentraderdev\dev" initialCatalog="MiTraderError" />
</HttpExceptionHandler>

Here's the code:

public class ErrorLogConfiguration : ConfigurationSection
{
    public static ErrorLogConfiguration GetConfig()
    {
        return ConfigurationManager.GetSection("HttpExceptionHandler\\errorLog") as ErrorLogConfiguration;
    }

    [ConfigurationProperty("initialCatalog", IsRequired = true)]
    public string InitialCatalog
    {
        get
        {
            return this["initialCatalog"] as string;
        }
    }

    [ConfigurationProperty("dataSource", IsRequired = true)]
    public string DataSource
    {
        get
        {
            return this["dataSource"] as string;
        }
    }
}

The return is always null. I've run out of ideas. Any help appreciated.


Solution

  • How about switching the slash direction:

    return ConfigurationManager.GetSection("HttpExceptionHandler/errorLog") as ErrorLogConfiguration;
    

    Here's a similar example from MSDN.