Example #1
0
        public void Install()
        {
            ServiceProcessInstaller spi = new ServiceProcessInstaller();

            spi.Account  = ServiceAccount.LocalSystem;
            spi.Username = null;
            spi.Password = null;
            spi.Context  = new InstallContext("install.txt", new string[0]);
            spi.Context.Parameters["assemblypath"] = GetType().Assembly.Location;
            AddInstaller(spi);
            spi.Install(new Hashtable());
        }
Example #2
0
        private static void InstallService(Options o)
        {
            var path = Assembly.GetEntryAssembly().Location;

            if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
            {
                o.Stayopen = true;
                Process.Start(new ProcessStartInfo(path, Parser.Default.FormatCommandLine(o))
                {
                    Verb = "runas"
                });
                o.Stayopen = false;
                return;
            }

            var spi = new ServiceProcessInstaller()
            {
                Context = new InstallContext(null, new string[] { $"/assemblypath=\"{path}\" --service" })
            };

            if (string.IsNullOrEmpty(o.Username))
            {
                spi.Account = ServiceAccount.LocalSystem;
            }
            else if (Enum.TryParse(o.Username, out ServiceAccount account))
            {
                spi.Account = account;
            }
            else
            {
                spi.Account  = ServiceAccount.User;
                spi.Username = o.Username;
                spi.Password = o.Password;
            }

            spi.Installers.Add(new ServiceInstaller()
            {
                ServiceName = nameof(WebRelay),
                DisplayName = nameof(WebRelay),
                Description = "Thin HTTP server used by WebRelay console app and shell extension.",
                StartType   = ServiceStartMode.Automatic
            });

            spi.Install(new ListDictionary());

            new ServiceController(nameof(WebRelay)).Start();
        }
Example #3
0
        static void Install(TextWriter writer, IDictionary state, ServiceProcessInstaller installer, ServiceInstaller serviceInstaller, object args)
        {
            var arr = (string[])args;

            writer.WriteLine("Install service " + arr[0]);

            installer.Context = serviceInstaller.Context = new InstallContext(null, null);
            serviceInstaller.Context.Parameters.Add("assemblyPath", IsDotnet2()
                ? (typeof(ServiceManager).Assembly.Location + "\" \"" + arr[0])
                : ("\"" + typeof(ServiceManager).Assembly.Location + "\" " + arr[0]));

            serviceInstaller.ServiceName = arr[0];
            serviceInstaller.DisplayName = arr[1] ?? "Nginx Daemon";
            serviceInstaller.Description = arr[2] ?? "Nginx daemon service & Tiny log rotate provider.";
            serviceInstaller.StartType   = ServiceStartMode.Automatic;

            installer.Install(state);
        }
Example #4
0
        public static void Install(Type exeType, IEnumerable <OpenService> services, string username, string password)
        {
            var spi = new ServiceProcessInstaller
            {
                Account  = string.IsNullOrEmpty(username) ? ServiceAccount.LocalSystem : ServiceAccount.User,
                Username = username,
                Password = password,
                Context  = new InstallContext("install.txt", new string[0])
            };

            spi.Context.Parameters["assemblypath"] = exeType.Assembly.Location;

            foreach (OpenService service in services)
            {
                service.AddInstaller(spi);
            }

            spi.Install(new Hashtable());
        }
Example #5
0
        public void Install()
        {
            RunAs(() =>
            {
                var state = new Hashtable();
                using (var installer = new ServiceProcessInstaller())
                    using (var serviceInstaller = new ServiceInstaller())
                    {
                        try
                        {
                            installer.Account = ServiceAccount.LocalSystem;
                            installer.Installers.Add(serviceInstaller);
                            Console.WriteLine("Install service " + serviceName);

                            installer.Context = serviceInstaller.Context = new InstallContext(null, null);
                            serviceInstaller.Context.Parameters.Add("assemblyPath", IsLowerDotnetFramework()
                            ? (typeof(ServiceManager).Assembly.Location + "\" \"" + serviceName)
                            : ("\"" + typeof(ServiceManager).Assembly.Location + "\" " + serviceName));

                            serviceInstaller.ServiceName = serviceName;
                            serviceInstaller.DisplayName = "Logrotate Daemon";
                            serviceInstaller.Description = "logrotate daemon service";

                            serviceInstaller.StartType = ServiceStartMode.Automatic;

                            installer.Install(state);
                            Console.Write("Success.");
                        }
                        catch (Exception e)
                        {
                            if (state.Count > 0)
                            {
                                installer.Rollback(state);
                            }
                            Console.Write("Failed: " + e.Message);
                        }
                    }
            });
        }