public Installer()
        {
            var process = new ServiceProcessInstaller
            {
                Account = ServiceAccount.LocalSystem,
            };

            var serviceAdmin = new ServiceInstaller
            {
                StartType        = ServiceStartMode.Automatic,
                DelayedAutoStart = true,
                ServiceName      = "GitHgMirrorService",
                DisplayName      = "Git-hg Mirror Service",
                Description      = "Runs the mirroring of the repositories.",
            };

            serviceAdmin.AfterInstall += (sender, e) =>
            {
                // Adding failure actions (i.e. what Windows should do when the service crashes) with delay in ms. Code
                // taken from https://code.msdn.microsoft.com/windowsdesktop/CSWindowsServiceRecoveryPro-2147e7ac/ Such
                // crashes should be very rare, but sometimes happen for some reason when there are a lot of errors
                // (like when GitHub goes down).
                var failureActions = new List <SC_ACTION>
                {
                    new SC_ACTION
                    {
                        Type  = (int)SC_ACTION_TYPE.RestartService,
                        Delay = 1000 * 60 * 15,
                    },
                    new SC_ACTION
                    {
                        Type  = (int)SC_ACTION_TYPE.RestartService,
                        Delay = 1000 * 60 * 60,
                    },
                    new SC_ACTION
                    {
                        Type  = (int)SC_ACTION_TYPE.RestartService,
                        Delay = 1000 * 60 * 120,
                    },
                };

                ServiceRecoveryProperty.ChangeRecoveryProperty(
                    scName: "GitHgMirrorService",
                    scActions: failureActions,
                    resetPeriod: 60 * 60 * 24 * 1,
                    command: "C:\\Windows\\System32\\cmd.exe /help /fail=%1%",
                    fFailureActionsOnNonCrashFailures: true,
                    rebootMsg: "reboot message");
            };
            Installers.Add(process);
            Installers.Add(serviceAdmin);
        }
Exemple #2
0
        public static void InstallService(string name)
        {
            string[] args_i = new string[] { name, "/LogFile=..\\log\\install.log" };
            ManagedInstallerClass.InstallHelper(args_i);
            Log.Info("The Enviriot service installed");

            List <SC_ACTION> FailureActions = new List <SC_ACTION>();

            // First Failure Actions and Delay (msec).
            FailureActions.Add(new SC_ACTION()
            {
                Type  = (int)SC_ACTION_TYPE.RestartService,
                Delay = 1000 * 15
            });

            // Second Failure Actions and Delay (msec).
            FailureActions.Add(new SC_ACTION()
            {
                Type  = (int)SC_ACTION_TYPE.RestartService,
                Delay = 1000 * 60 * 2
            });

            // Subsequent Failures Actions and Delay (msec).
            FailureActions.Add(new SC_ACTION()
            {
                Type  = (int)SC_ACTION_TYPE.None,
                Delay = 1000 * 60 * 3
            });

            // Configure service recovery property.
            ServiceRecoveryProperty.ChangeRecoveryProperty("Enviriot", FailureActions, 60 * 60 * 24, "", false, "");
            Log.Info("The service recovery property is modified successfully");
            ServiceController svc = new ServiceController("Enviriot");

            svc.Start();
            svc.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 3));
        }