/// <summary> /// Installs the service and starts it immediatelly after if non-null arguments are passed in. /// </summary> /// <param name="args">Command line arguments for the service initialization.</param> /// <returns>Tool error code.</returns> public static int InstallService(string[] args = null) { if (IsInstalled()) { return((int)ToolError.NoError); } if (args != null) { Configuration.SetCommandLineArguments(new Hashtable(2) { { "autostart", true }, { "args", string.Join("|", args) } }); } try { using (Installer installer = GetInstaller()) { IDictionary state = new Hashtable(); try { installer.Install(state); installer.Commit(state); } catch { try { installer.Rollback(state); } catch { return((int)ToolError.InstallServiceNotRollBacked); } return((int)ToolError.InstallService); } } } catch { return((int)ToolError.InstallService); } return((int)ToolError.NoError); }
public void Install() { var state = new Hashtable(); try { _installer.Install(state); _installer.Commit(state); } catch { try { _installer.Rollback(state); } catch { } throw; } }
public static bool InstallOrUpdateService(XElement xElement, string ServiceProcess, string AgentAddress, string logfile) { try { var ServiceLabel = xElement.xLabel(xElement.Name.LocalName); var ServiceDescription = xElement.xDescription(); var ServiceName = AgentInstancePrefix + xElement.xKey(); var ServiceArgs = new string[0]; var ServicesDependedOn = new string[0]; var ServiceLogon = ServiceAccount.NetworkService; var ServiceStartMode = xElement.xAdminState() == "disabled" ? System.ServiceProcess.ServiceStartMode.Disabled : System.ServiceProcess.ServiceStartMode.Manual; switch (xElement.Name.LocalName) { case "agent": ServiceLabel = "Urbetrack Agent (" + ServiceLabel + ")"; ServiceName = AgentServiceName; ServiceLogon = ServiceAccount.LocalSystem; ServiceArgs = new[] { "-A" }; ServiceStartMode = System.ServiceProcess.ServiceStartMode.Automatic; break; case "instance": ServiceLabel = "Urbetrack Instance (" + ServiceLabel + ")"; ServiceArgs = new[] { "-I", xElement.xKey(), AgentAddress }; ServicesDependedOn = new[] { AgentServiceName }; break; } var serviceController = GetWindowsService(ServiceName); /////// UPDATE if (serviceController != null) { if (serviceController.DisplayName != ServiceLabel) { serviceController.DisplayName = ServiceLabel; } try { if (ServiceStartMode == ServiceStartMode.Automatic && GetServiceStart(serviceController.ServiceName) != ServiceStartType.Automatic) { SetServiceStart(serviceController.ServiceName, ServiceStartType.Automatic); } if (ServiceStartMode == ServiceStartMode.Disabled && GetServiceStart(serviceController.ServiceName) != ServiceStartType.Disabled) { SetServiceStart(serviceController.ServiceName, ServiceStartType.Disabled); } } catch (ApplicationException e) { T.EXCEPTION(e); } return(true); } //////// INSTALL var path = String.Format("/assemblypath={0}", ServiceProcess); String[] cmdline = { path }; var Context = new InstallContext(logfile, cmdline); var Runner = new Installer { Context = Context }; var ProcesServiceInstaller = new ServiceProcessInstaller { Context = Context, Account = ServiceLogon, Username = null, Password = null, CmdLineArgs = ServiceArgs }; var ServiceInstallerObj = new ServiceInstaller { Context = Context, DisplayName = ServiceLabel, Description = ServiceDescription, ServiceName = ServiceName, ServicesDependedOn = ServicesDependedOn, StartType = ServiceStartMode, StartOnInstall = false }; ServiceInstallerObj.FailureActions.Add(new FailureAction(RecoverAction.Restart, 60000)); ServiceInstallerObj.FailureActions.Add(new FailureAction(RecoverAction.Restart, 60000)); ServiceInstallerObj.FailureActions.Add(new FailureAction(RecoverAction.Restart, 60000)); ServiceInstallerObj.FailCountResetTime = 60 * 60 * 24; // 1 dia. Runner.Installers.Add(ProcesServiceInstaller); Runner.Installers.Add(ServiceInstallerObj); var state = new System.Collections.Specialized.ListDictionary(); try { Runner.Install(state); Runner.Commit(state); } catch (Exception e) { Context.LogMessage(Format.Join(Format.Exception(e, "Ejecutando el instaldor"))); Runner.Rollback(state); } return(true); } catch (Exception e) { T.EXCEPTION(e); return(false); } }
private void InstallService(WinServiceInstaller group) { try { var serviceKey = group.ServiceNamePrefix + ServiceName; Console.WriteLine("Installing: {0}", serviceKey); var serviceController = GetWindowsService(serviceKey); if (serviceController != null) { Console.WriteLine(" Already Installed."); return; } var installLogFile = Path.GetTempFileName(); Console.WriteLine("Log: {0}", installLogFile); var context = new InstallContext(installLogFile, new[] { String.Format("/assemblypath={0}", Process.GetCurrentProcess().MainModule.FileName.Replace("vshost.", "")) }); var runner = new Installer { Context = context }; var procesServiceInstaller = new ServiceProcessInstaller { Context = context, Account = ServiceAccount, Username = null, Password = null, CmdLineArgs = new[] { String.Format("{0} {1}", HostApplication, serviceKey) } }; var serviceInstallerObj = new ServiceInstaller { Context = context, DisplayName = DisplayName, Description = Description, ServiceName = serviceKey, ServicesDependedOn = default(string[]), StartType = StartMode, StartOnInstall = false, FailRunCommand = FailureRunCommand }; if (FirstFailureAction != null) { serviceInstallerObj.FailureActions.Add(FirstFailureAction); } if (SecondFailureAction != null) { serviceInstallerObj.FailureActions.Add(SecondFailureAction); } if (SubsequentFailuresAction != null) { serviceInstallerObj.FailureActions.Add(SubsequentFailuresAction); } serviceInstallerObj.FailCountResetTime = default(int); // 1 dia. runner.Installers.Add(procesServiceInstaller); runner.Installers.Add(serviceInstallerObj); var state = new ListDictionary(); try { runner.Install(state); runner.Commit(state); } catch (Exception e) { Console.WriteLine("Exception: " + e); runner.Rollback(state); } } catch (Exception e) { Console.WriteLine("Exception: " + e); } }