代码之家  ›  专栏  ›  技术社区  ›  AlphaTry

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

  •  0
  • AlphaTry  · 技术社区  · 7 年前

    我可以手动成功地启动/停止服务,但在系统重新启动时无法启动,尽管启动类型设置为 '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服务作为依赖项添加到项目/服务安装程序中,设置 DelayedAutoStart = true . 不管我在网上读到什么解决方案,我都试过了,但服务不会在重新启动时启动。

    接下来我将添加服务的起始点/入口点片段。

    程序.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.");
        }
    
    }
    

    投影安装程序 -包括 System.ServiceProcess.ServiceProcessInstaller System.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});
    }
    

    记录点 -在手动启动/停止时,我会得到以下日志点,但当我重新启动系统时,不会出现任何问题

    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 回复  |  直到 7 年前