Search code examples
c#production-environmentconfigurationmanager

configurationmanager exception on server but does work on dev machine


This problem haunted me for many hours now.
I have a LINQ query to parse an undefined number of email adresses in a config file in my application.
Following code will help tp understand:

App.config
<configuration>
  <appSettings>
    <add key="errorMail1" value="support@mail.com"/>
    <add key="errorMail2" value="test@mail.com"/>
  </appSettings>
<configuration>

So when I read the email addresses, I do it the following way through a property:

private List<string> _mailsWhenProblems;
public List<string> mailsWhenProblems
{
    get
    {
        if (_mailsWhenProblems == null)
        {
            var keys = ConfigurationManager.AppSettings.Keys;
            _mailsWhenProblems = keys.Cast<object>()
                .Where(key => key.ToString().ToLower()
                .Contains("errorMail".ToLower()))
                .Select(key => ConfigurationManager.AppSettings.Get(key.ToString())).ToList();
        }
        return _mailsWhenProblems;
    }
    set
    {
        _mailsWhenProblems = value;
    }
}

That said, as long as I run it in Debug mode on dev machine, everything works perfectly as expected. But as soon as I deploy it on Windows Server 2012, the application crashes with this Exception:

************** Texte de l'exception **************
System.Configuration.ConfigurationErrorsException: Échec de l'initialisation du système de configuration ---> System.Configuration.ConfigurationErrorsException: Section de configuration non reconnue oracle.manageddataaccess.client. (C:\Program Files\celibec\Celibec Transfert Android FileWatcher\FileWatcherService.exe.Config line 20)
   à System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   à System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   à System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- Fin de la trace de la pile d'exception interne ---
   à System.Configuration.ConfigurationManager.PrepareConfigSystem()
   à System.Configuration.ConfigurationManager.get_AppSettings()

Solution

  • The exception has nothing to do with your code. You probably forgot the <configSections> tag in the config file on your server. With this tag you register the custom oracle.manageddataaccess.client tag. I guess you have this tag on your dev machine.
    Please note, that <configSections> has to be the first in the <configuration> tag.

    For more information see this MSDN link about Custom Configuration Sections.