1

I have a Window Service that runs Scheduler to sends emails.

If i debug Window Service it is working fine and send emails as expected but after installation it is not working and has error in Log file.

Edited I have attached App.Config file for better understanding

Log4net file:

020-08-11 18:34:00,158 INFO [4] E4U_S_Type.Scheduler.E4U_Scheduler.OnStart - +++++++++ Service Started ++++++++++++++
2020-08-11 18:34:00,807 INFO [4] E4U_S_Type.BI.Services.Scheduler.start - ################### Starting Initialization of Scheduler ###################
2020-08-11 18:34:00,934 ERROR [4] E4U_S_Type.BI.Services.Scheduler.start - Configuration system failed to initialize

Service.cs

 protected override void OnStart(string[] args)
        {
            try
            {
                log.Info("+++++++++ Service Started ++++++++++++++");
                Bootstrapper.Init();
                BI.Services.Scheduler sc = new BI.Services.Scheduler();
                sc.start();
            }
            catch (Exception ex)
            {
                log.Error("Service Error" + ex.Message);
            }
            
        }
       

        protected override void OnStop()
        {
            log.Info("----------- Service Stop -----------");
        }

Scheduler.cs

public class Scheduler
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public Scheduler()
        {
            
        }
        public void start()
        {
            try
            {

                log.Info("################### Starting Initialization of Scheduler ###################");

                // construct a scheduler factory
                ISchedulerFactory schedulerFactory = new StdSchedulerFactory();

                // get a scheduler
                IScheduler scheduler = schedulerFactory.GetScheduler();
                scheduler.Start();
            }
            catch(Exception ex)
            {
              log.error(ex.Message);
             }

App.Config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </sectionGroup>
  </configSections>

  <log4net>
    <appender name="TestAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\WinServices\WinServiceLogger.log" />
      <encoding value="utf-8" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level [%thread] %type.%method - %message%n" />
      </layout>
    </appender>
    <root>
      <level value="All" />
      <appender-ref ref="TestAppender" />
    </root>
  </log4net>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Syncfusion.Compression.Portable" publicKeyToken="3d67ed1f87d44c89" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-17.3120.0.28" newVersion="17.3120.0.28" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Syncfusion.OfficeChart.Portable" publicKeyToken="3d67ed1f87d44c89" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-17.3120.0.28" newVersion="17.3120.0.28" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Syncfusion.Licensing" publicKeyToken="632609b4d040f6b4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-17.3451.0.14" newVersion="17.3451.0.14" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
walen
  • 7,103
  • 2
  • 37
  • 58
Novapex
  • 45
  • 1
  • 5
  • Check if this could help https://stackoverflow.com/questions/6436157/configuration-system-failed-to-initialize#answer-6472696 – Sowmyadhar Gourishetty Aug 11 '20 at 14:10
  • Start by logging the full exception: log.error(ex) – jeroenh Aug 11 '20 at 14:10
  • ````2020-08-11 19:12:29,960 ERROR [4] E4U_S_Type.BI.Services.Scheduler.start - System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Only one element allowed per config file and if present must be the first child of the root element. (E:\Development\Workspace\Saudkhan\e4uservices\E4U S-Type.Scheduler\bin\Release\E4U S-Type.Scheduler.exe.Config line 6) ```` – Novapex Aug 11 '20 at 14:16
  • I have added App.Config file – Novapex Aug 11 '20 at 14:19
  • It might be referring to any other configs? Did you see any other configs in the parent folders? – Sowmyadhar Gourishetty Aug 11 '20 at 14:34
  • No there is only one Config file – Novapex Aug 11 '20 at 14:57

1 Answers1

0

The config section must be the first element because it just add extra configuration that may be not a native configuration (from the machine.config). So, as we can see, you are just adding the sections for Entity Framework and log4net components. It must be the first child tag of the root, for configuration purpose. The error message already said that:

Only one element allowed per config file and if present must be the first child of the root element.

Move the configSections to the top of the App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </sectionGroup>
  </configSections>

...

Tip: use the EventViewer to log your Windows Service, it is helpful.

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194