示例#1
0
        public static void Send(string args)
        {
            var client = ControlPipeFactory.CreateClient("instance");

            try
            {
                client.Connect(1);
                var writer = new StreamWriter(client)
                {
                    AutoFlush = true
                };
                writer.WriteLine(string.IsNullOrEmpty(args) ? "-?" : args);
                client.WaitForPipeDrain();
                Console.Write(new StreamReader(client).ReadToEnd());
            }
            catch (System.TimeoutException)
            {
                Console.Error.Write("connect nginx daemon service timeout.");
            }
        }
示例#2
0
        static void Invoke(InvokeAction action, object args)
        {
            const string PipeType = "controller";

            var user = WindowsIdentity.GetCurrent();

            if (!new WindowsPrincipal(user).IsInRole(WindowsBuiltInRole.Administrator))
            {
                ProcessStartInfo si = new ProcessStartInfo();
                si.WindowStyle      = ProcessWindowStyle.Hidden;
                si.UseShellExecute  = true;
                si.FileName         = typeof(ServiceManager).Assembly.Location;
                si.WorkingDirectory = Environment.CurrentDirectory;
                si.Arguments        = Environment.CommandLine.Substring(Environment.CommandLine.IndexOf(' ') + 1);
                si.Verb             = "runas";

                using (var server = ControlPipeFactory.CreateServer(PipeType))
                {
                    server.BeginWaitForConnection(OnPipeConnected, server);

                    try
                    {
                        Process.Start(si).WaitForExit();
                    }
                    catch
                    {
                        Console.WriteLine("Install/Uninstall service requires administrator priviledge.");
                        Environment.Exit(1);
                    }
                }
            }
            else
            {
                var        pipe = ControlPipeFactory.CreateClient(PipeType);
                TextWriter writer;
                try
                {
                    pipe.Connect(1);
                    writer = new StreamWriter(pipe)
                    {
                        AutoFlush = true
                    };
                }
                catch (System.TimeoutException)
                {
                    writer = Console.Out;
                }

                var state = new Hashtable();

                using (var installer = new ServiceProcessInstaller())
                    using (var serviceInstaller = new ServiceInstaller())
                    {
                        try
                        {
                            installer.Account = ServiceAccount.LocalSystem;
                            installer.Installers.Add(serviceInstaller);
                            action(writer, state, installer, serviceInstaller, args);
                            if (state.Count > 0)
                            {
                                installer.Commit(state);
                            }
                            writer.Write("Success.");
                        }
                        catch (Exception e)
                        {
                            if (state.Count > 0)
                            {
                                installer.Rollback(state);
                            }
                            writer.WriteLine("Error: " + e.Message);
                            Environment.Exit(1);
                        }
                    }
            }
            Environment.Exit(0);
        }