重新启动后我的Windows服务无法启动

时间:2019-05-30 16:17:19

标签: c# windows-services

我可以手动成功启动/停止服务,但是尽管启动类型设置为'Automatic',但是在系统重启时它无法启动。在某些终端上,重新启动后,服务无法启动并显示错误failed to connect DB

29052019 17:25:27 ERROR Logs.TransactionLogManager - Exception Thrown in getNewSession()

29052019 17:25:27 ERROR Logs.TransactionLogManager - Unable To Open Database Session

29052019 17:25:27 ERROR Logs.TransactionLogManager - Unable to complete network request to host "localhost".
   at FirebirdSql.Data.FirebirdClient.FbConnectionInternal.Connect()
   at FirebirdSql.Data.FirebirdClient.FbConnectionPoolManager.Pool.CreateNewConnectionIfPossibleImpl(FbConnectionString connectionString)
   at FirebirdSql.Data.FirebirdClient.FbConnectionPoolManager.Pool.GetConnection(FbConnection owner)
   at FirebirdSql.Data.FirebirdClient.FbConnection.Open()
   at NHibernate.Connection.DriverConnectionProvider.GetConnection()
   at NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare()
   at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper)
   at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactory sessionFactory)
   at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
   at NHibernate.Cfg.Configuration.BuildSessionFactory()
   at StevensCommon.Database.FluentNHibernateManager.OpenNewSession()
   at StevensCommon.DAOs.StevensDAO.GetNewSession()

在开发环境中进行调试时,我什至没有遇到任何错误。假设firebird服务确实在我的服务之前启动,但是仍然无法在重新启动时启动该服务。日志中什么也没有。

我在代码中添加了一些日志点,当我手动停止/启动服务时,它们都会被记录下来。但是,当系统重新启动并且服务器永不启动时,日志中什么也没有显示。

尝试将Firebird Service作为依赖项添加到Project / Service Installer中,设置DelayedAutoStart = true。无论我在网上阅读任何解决方案,我都尝试过,但是服务无法在重启时启动。

下面,我要添加服务的起点/起点片段。

Program.cs

static class Program
{
  static void Main(string[] args)
  {
    TransactionLogManager.WriteServiceLog("FLAG - Before DAO Call", null);
    // Check to see if we have a licence 
    if (LicenceFileDAO.GetLicence() != null && LicenceManager.IsLicenceActive())
    {
        TransactionLogManager.WriteServiceLog("FLAG - Inside DAO Block", null);
        //Run the IS Service
        ServiceBase[] ServicesToRun = new ServiceBase[] { new MyService() };
        ServiceBase.Run(ServicesToRun);
        TransactionLogManager.WriteServiceLog("FLAG - End of DAO Call", null);
    }
    else
    {
        TransactionLogManager.WriteServiceLog("No Valid Licence Found - Either A Licence Has Not Been Activated Or It Has Expired. Please Contact Support", null);
    }
  }
}

MyService.cs

public partial class MyService : ServiceBase
{
    protected static Timer importTimer = null;
    protected static Timer exportTimer = null;
    protected static Timer licenceTimer = null;
    protected static Timer requestTimer = null;
    protected static Timer uploadTimer = null;
    protected static Timer handshakeTimer = null;
    protected static Timer scheduledReportTimer = null;
    protected static Timer alertTimer = null;
    public static Boolean ImportRunning = false;
    public static Boolean ExportRunning = false;
    public static Boolean RequestRunning = false;
    public static Boolean UploadRunning = false;
    public static Boolean HandshakeRunning = false;
    public static Boolean ScheduledReportRunning = false;
    public static Boolean AlertReportRunning = false;
    public static int? zynkWorkflowProcessId = null;

    public MyService()
    {
        TransactionLogManager.WriteServiceLog("FLAG - 1", null);

        InitializeComponent();

        InitializeServiceTimers();


        try
        {
            MessengerService.Start();
            TransactionLogManager.WriteServiceLog("Messaging Service Started", null);
        }
        catch (Exception e)
        {
            TransactionLogManager.WriteServiceLog(e.Message, e.StackTrace);
        }
    }

    private void InitializeComponent()
    {
        this.eventLog1 = new System.Diagnostics.EventLog();
        ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
        // 
        // MyService
        // 
        this.ServiceName = "MyService";
        ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();

    }

    private void InitializeServiceTimers()
    {
        if (!System.Diagnostics.EventLog.SourceExists("Stevens Integration Service"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "Stevens Integration Service", "ServiceLog");
        }
        eventLog1.Source = "Stevens Integration Service";
        eventLog1.Log = "ServiceLog";

        TransactionLogManager.WriteServiceLog("FLAG - 2", null);

        importTimer = new Timer(IntegrationServiceSettings.GetImportInterval() * 1000);
        exportTimer = new Timer(IntegrationServiceSettings.GetExportInterval() * 1000);
        licenceTimer = new Timer(86400 * 1000); // Daily
        requestTimer = new Timer(IntegrationServiceSettings.GetRequestInterval() * 1000);
        scheduledReportTimer = new Timer(30000);
        alertTimer = new Timer(20000);

        importTimer.Elapsed += new ElapsedEventHandler(ImportTimerElapsed);
        exportTimer.Elapsed += new ElapsedEventHandler(ExportTimerElapsed);
        licenceTimer.Elapsed += new ElapsedEventHandler(LicenceTimerElapsed);
        requestTimer.Elapsed += new ElapsedEventHandler(RequestTimerElapsed);
        scheduledReportTimer.Elapsed += new ElapsedEventHandler(ScheduledReportTimerElapsed);
        alertTimer.Elapsed += new ElapsedEventHandler(AlertTimerElapsed);
        TransactionLogManager.WriteServiceLog("FLAG - 3", null);
        StartTimers();
        TransactionLogManager.WriteServiceLog("FLAG - 4", null);
    }

    protected override void OnStart(string[] args)
    {
        eventLog1.WriteEntry("Stevens Integration Service Starting...");
        TransactionLogManager.WriteServiceLog("FLAG - OnStart() Started", null);

        if (StevensDAO.TestConnection())
        {
            eventLog1.WriteEntry("Configure Settings...");
            IntegrationServiceSettings.InitialiseAll();
            eventLog1.WriteEntry("Done.");
        }
        TransactionLogManager.WriteServiceLog("FLAG - OnStart() Finished", null);


        TransactionLogManager.WriteServiceLogInfo("Started");
        eventLog1.WriteEntry("Started.");
    }

}

ProjectInstaller -包括System.ServiceProcess.ServiceProcessInstallerSystem.ServiceProcess.ServiceInstaller


private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
private MyService myService ;

private void InitializeComponent()
{
    this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
    this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
    this.myService = new IntegrationService.MyService();

    // serviceProcessInstaller1
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.serviceProcessInstaller1.Password = null;
    this.serviceProcessInstaller1.Username = null;

    // serviceInstaller1
    this.serviceInstaller1.Description = "My Integration Service";
    this.serviceInstaller1.DisplayName = "MyService";
    this.serviceInstaller1.ServiceName = "MyService";
    //this.serviceInstaller1.ServicesDependedOn = new string[] { "Firebird Guardian - DefaultInstance", "Firebird Server - DefaultInstance" };
    //this.serviceInstaller1.DelayedAutoStart = true;
    this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;

    // myService 
    this.myService.ExitCode = 0;
    this.myService.ServiceName = "MyService";

    // ProjectInstaller
    this.Installers.AddRange(new System.Configuration.Install.Installer[] {
    this.serviceProcessInstaller1,
    this.serviceInstaller1});
}

LogPoints -在手动启动/停止时,我确实获得了以下日志点,但是当我重新启动系统时,没有任何反应

30052019 16:19:35 ERROR Logs.TransactionLogManager - FLAG - Before DAO Call
30052019 16:19:35 ERROR Logs.TransactionLogManager - FLAG - Inside DAO Block
30052019 16:19:35 ERROR Logs.TransactionLogManager - FLAG - 1
30052019 16:19:35 ERROR Logs.TransactionLogManager - FLAG - 2
30052019 16:19:35 ERROR Logs.TransactionLogManager - FLAG - 3
30052019 16:19:35 ERROR Logs.TransactionLogManager - FLAG - 4
30052019 16:19:35 ERROR Logs.TransactionLogManager - Messaging Service Started
30052019 16:19:35 ERROR Logs.TransactionLogManager - FLAG - OnStart() Started
30052019 16:19:35 ERROR Logs.TransactionLogManager - Client 1 is now connected!
30052019 16:19:36 ERROR Logs.TransactionLogManager - FLAG - OnStart() Finished

0 个答案:

没有答案