Windows Service Fail to start after system restart RRS feed

  • Question

  • I can successfully Start/Stop service Manually but it fails to start on a system restart although startup type is set to Automatic. On some terminal, when rebooted, service fail to start with error "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()

    On development environment whilst debugging I'm not even getting any error. Assuming that on my machine the firebird services does start up before my service, still it fails to start the service on a reboot. And nothing comes up in the log.

    I've added some log points within the code and they all gets logged when I Stop/Start the service manually. But nothing shows up in the log when system is rebooted and service never gets started.

    Tried adding Firebird Service as a dependencies in the Project/Service Installer, set the DelayedAutoStart = true. Whatever solution I read online I've tried it but service is not starting on a reboot.

    Following I'm adding starting/entry point snippet of the service.


    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() };
            TransactionLogManager.WriteServiceLog("FLAG - End of DAO Call", null);
            TransactionLogManager.WriteServiceLog("No Valid Licence Found - Either A Licence Has Not Been Activated Or It Has Expired. Please Contact Support", null);


    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);
                TransactionLogManager.WriteServiceLog("Messaging Service Started", null);
            catch (Exception e)
                TransactionLogManager.WriteServiceLog(e.Message, e.StackTrace);
        private void InitializeComponent()
            this.eventLog1 = new System.Diagnostics.EventLog();
            // MyService
            this.ServiceName = "MyService";
        private void InitializeServiceTimers()
            if (!System.Diagnostics.EventLog.SourceExists("Stevens Integration Service"))
                    "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);
            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...");
            TransactionLogManager.WriteServiceLog("FLAG - OnStart() Finished", null);

    PojectInstaller -  That Includes  System.ServiceProcess.ServiceProcessInstaller and 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[] {

    LogPoints - On manual Start/Stop I do get following log points, but when I reboot the system nothing comes up

    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

    Friday, May 31, 2019 12:44 PM

All replies

  • The exception is occurring while trying to connect to your remote DB. There are a couple of possibilities.

    1) You have a dependency issue. If your service relies on another service (or the network) then you need to modify your service installer to list the dependent services. Windows will then defer starting your service until those services are started. At a minimum you generally need to wait for the network to come online.

    To test this scenario, after Windows is running start the service via SCM or the command line. If it starts then this is likely the issue.

    2) You have a permissions issue. Your service will connect based upon the connection string you're using. For services they tend to run with a local account. Therefore you wouldn't be able to access a network resource. At a minimum you'd need to configure your service to run as network service and then ensure that the machine name has permissions to access the remote service. More likely though is you'll use a domain account that already has permissions.

    To test this scenario start your service via SCM. If it fails then it is likely a permissions issue.

    Michael Taylor

    Friday, May 31, 2019 1:41 PM
  • The exception is occurring while trying to connect to your remote DB. There are a couple of possibilities.

    1) You have a dependency issue. If your service relies on another service (or the network) then you need to modify your service installer to list the dependent services. Windows will then defer starting your service until those services are started. At a minimum you generally need to wait for the network to come online.

    To test this scenario, after Windows is running start the service via SCM or the command line. If it starts then this is likely the issue.

    Like you said, after Windows is running Starting service via SCM does starts the service.

    If you notice, I've also tried listing the dependent services 

    // serviceInstaller1
    this.serviceInstaller1.ServicesDependedOn = new string[] { "Firebird Guardian - DefaultInstance", "Firebird Server - DefaultInstance" };

    But this also doesn't make my service dependent on Firbird's services. When I install the service, I don't see those services in Dependencies Tab in MyService's properties.

    I've also tried setting StartType = Automatic (Delayed) manually. Now restarting the system does starts the service. So when I set the StatType to Automatic (Delayed) in code, and expect it at the time when service is being installed. It doesn't work and still shows Automatic.
    I'm using following code to make it Automatic Delayed.

    this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
    this.serviceInstaller1.DelayedAutoStart = true;

    I believe if i could make MyService dependent on firebird services successfully, It will bring the service up after reboot.

    Following are the service MyService Depends on.

    Service Name                                      DisplayName

    FirebirdGuardianDefaultInstance            Firebird Guardian - DefaultInstance

    FirebirdServerDefaultInstance                Firebird Server - DefaultInstance

    Friday, May 31, 2019 2:17 PM
  • The `ServicesDependedOn` property needs to use the service names, not their display names.

    serviceInstaller1.ServicesDependedOn = new [] {

    Michael Taylor

    Friday, May 31, 2019 3:16 PM
  • Thanks Michael for your reply but You see, this the first time I'm working with Windows Services. I tried the Service Name before i changed it to use display name. Neither work for me .I.E I don't see anything in the Dependencies Tab. And Service doesn't comes up.

    Apart from this is there any other approach to list dependent services, And also about setting Start Type to Automatic Delayed. As mentioned in my previous reply
    Friday, May 31, 2019 3:41 PM
  • Just out of curiosity, how are you installing the service? Are you using InstallUtil? Are you uninstalling the service first? There is a long-standing issue in Windows where you cannot fully uninstall a service if the service is running. It'll get removed but is still installed. Until you reboot it won't recover. So the way you'll need to test your installer is.

    1. Stop any running instance. 

    2. Uninstall the service.

    3. Install the service via InstallUtil.

    # Powershell
    stop-service serviceName
    InstallUtil /u serviceAssembly
    InstallUtil serviceAssembly

    Michael Taylor

    Friday, May 31, 2019 3:49 PM
  • The product I'm working on has a service and serviceUI aswell that just shows the stat and to Start Stop service through UI. Also to set some DB parameters through UI.

    I'm using wixnetfxextension (Wix Toolset) for installation. Service gets installed through by Wizard. 

    • Edited by fijju Friday, May 31, 2019 4:30 PM Detail added
    Friday, May 31, 2019 4:28 PM
  • I see. Since the issue is with installing your service and you're using a third party installer (Wix) then I think you need to post this in their forums. From my understanding WiX doesn't use ServiceInstaller or any of the built in .NET stuff. Therefore the issue would be a bad WiX configuration and not your installer code.

    You can confirm this by using InstallUtil to install the service directly (instead of WiX), after you've uninstalled first. If, after running InstallUtil, the service is properly configured then it is a WiX issue.

    Michael Taylor

    Friday, May 31, 2019 4:37 PM