noplacelike127.0.0.1
noplacelike127.0.0.1

Reputation: 41

Configuration system failed to initialize at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)

Created a C# Service Project in Visual Studio 2015, putting it together from various code sources.

It compiles, I can install the service, and I can start the service, but the service won’t fire the logic. It is supposed to look at a particular folder for only XML files, then upload them to an FTP site.

I added a logger and it writes out the following error consistently:

FTP Upload Service Started 05/08/2016 10:44:42 AM FTP Upload Service Error on: 05/08/2016 10:44:42 AM Configuration system failed to initialize at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey) at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName) at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) at System.Configuration.ConfigurationManager.get_AppSettings() at FTPUploadService.Service1.ScheduleService() in C:\user\Desktop\FTPUploadService\FTPUploadService\FTPUploadService\Service1.cs:line 51

I go to this particular line in the code (line 51), which is:

string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();

For reference here is more of the code block:

try
        {
            Scheduler = new Timer(new TimerCallback(SchedulerCallback));
            string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
            this.WriteToFile("FTP Upload Service Mode: " + mode + " {0}");

            //Set the Default Time.
            DateTime scheduledTime = DateTime.MinValue;

            if (mode == "DAILY")
            {
                //Get the Scheduled Time from AppSettings.
                scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
                if (DateTime.Now > scheduledTime)
                {
                    //If Scheduled Time is passed set Schedule for the next day.
                    scheduledTime = scheduledTime.AddDays(1);
                }
            }

            if (mode.ToUpper() == "INTERVAL")
            {
                //Get the Interval in Minutes from AppSettings.
                int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);

                //Set the Scheduled Time by adding the Interval to Current Time.
                scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);
                if (DateTime.Now > scheduledTime)
                {
                    //If Scheduled Time is passed set Schedule for the next Interval.
                    scheduledTime = scheduledTime.AddMinutes(intervalMinutes);
                }
            }

I’ve tried quite a few things, changing the XML format of the app.config, making sure the namespaces that are needed are there, checking the syntax of the XML for the app.config, to make sure it is correct…none have helped. Here is the app.config:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="Mode" value="Interval"/>
    <!-- <add key ="Mode" value ="Interval"/>-->
    <add key="IntervalMinutes" value="1"/>
    <add key="ScheduledTime" value="18:41"/>
    <add key="sourceFiles" value="C:\XML Test\"/>
    <add key="sourcePath" value="C:\XML Test\"/>
    <add key="files" value="*.xml"/>
    <add key="allDirectories" value="True"/>
    <add key="addDateTimeBeforeUpload" value="True"/>
    <add key="hostName" value="XXXXXXXXX"/>
    <add key="Port" value="22"/>
    <add key="UserName" value="XXXXXXX"/>
    <add key="Password" value="XXXXXXXX"/>
    <add key="SshHostKeyFingerprint" value=""/>
    <add key="uploadpath" value="/XXXXXXX/"/>
    <add key="removeSourceFiles" value="false"/>
    <add key="SshPrivateKeyPath" value=""/>
    <add key="protocol" value=""/>
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
    </configuration>

Any suggestions?? Issue with Configuration Manager?

Upvotes: 1

Views: 7857

Answers (1)

cstopher
cstopher

Reputation: 261

I've run into this issue before and it is most likely because the xml in your app.config is not setup properly I believe the "configSections" node must be the first child node after "configuration". Check out my xml below and give that a try.

<?xml version="1.0"?>
<configuration>
 <configSections>
   <sectionGroup name="applicationSettings"type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
   </sectionGroup>
 </configSections>
 <appSettings>
   <add key="Mode" value="Interval"/>
   <!-- <add key ="Mode" value ="Interval"/>-->
   <add key="IntervalMinutes" value="1"/>
   <add key="ScheduledTime" value="18:41"/>
   <add key="sourceFiles" value="C:\XML Test\"/>
   <add key="sourcePath" value="C:\XML Test\"/>
   <add key="files" value="*.xml"/>
   <add key="allDirectories" value="True"/>
   <add key="addDateTimeBeforeUpload" value="True"/>
   <add key="hostName" value="XXXXXXXXX"/>
   <add key="Port" value="22"/>
   <add key="UserName" value="XXXXXXX"/>
   <add key="Password" value="XXXXXXXX"/>
   <add key="SshHostKeyFingerprint" value=""/>
   <add key="uploadpath" value="/XXXXXXX/"/>
   <add key="removeSourceFiles" value="false"/>
   <add key="SshPrivateKeyPath" value=""/>
   <add key="protocol" value=""/>
 </appSettings>
 <startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
 </startup>
</configuration>

Upvotes: 6

Related Questions