如何解决窗口服务问题

问题描述

我有一个运行Scheduler的Windows服务来发送电子邮件。

如果我调试Window Service,它可以正常工作并按预期方式发送电子邮件,但安装后它将无法正常工作,并且日志文件中有错误。

已编辑 我已附加App.Config文件以更好地了解

Log4net文件:

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,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>

解决方法

config部分必须是第一个元素,因为它只是添加了可能不是本机配置的其他配置(来自machine.config)。因此,正如我们所看到的,您只是添加了use Text::CSV_XS; use autodie; open my $out,q{>},q{myFile}; # All of these input strings are parsed correctly,unlike when using "split": # my $run_test = q{sydney,delhi,NY,Russia}; # my $run_test = q{sydney,Russia,"field,with,commas"}; my $run_test = q{sydney,commas",missing,quote}; # binary => 1 : enable parsing binary characters in quoted fields. # auto_diag => 1 : print the internal error code and the associated error message to STDERR. my $csv = Text::CSV_XS->new( { binary => 1,auto_diag => 1 } ); if ( $csv->parse( $run_test ) ) { print {$out} map { "$_\n" } $csv->fields; } else { print STDERR q{parse() failed on: },$csv->error_input,qq{\n}; } Entity Framework组件的部分。出于配置目的,它必须是根目录的第一个子标记。错误消息已经表明:

每个配置文件只允许一个元素,如果 present必须是根元素的第一个子元素。

log4net移至App.config的顶部:

configSections

提示:使用EventViewer记录Windows服务,这很有帮助。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...