コード例 #1
0
ファイル: Program.cs プロジェクト: zaigr/epam-mentoring-a2
        public static void Main()
        {
#if DEBUG
            ServiceLocator.Start();

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();

            return;
#endif

            var serviceOptions = GetServiceOptions();

            var installer = new WindowsServiceInstaller();
            if (installer.IsServiceExists(serviceOptions.ServiceName))
            {
                Console.WriteLine($"Service '{serviceOptions.ServiceName}' already installed. Stop and remove it.");

                installer.Stop(serviceOptions.ServiceName);
                installer.Uninstall(serviceOptions);
            }

            Console.WriteLine($"Install and start '{serviceOptions.ServiceName}' service");

            installer.Install(serviceOptions);
            installer.Start(serviceOptions.ServiceName);

            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Application.cs プロジェクト: luoyiminga/ServerX
        public bool StartHost(int port, bool local = false)
        {
            //UninstallService();
            if (!local)
            {
                File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServiceParams.txt"),
                                  new JavaScriptSerializer().Serialize(new ServiceInstallParams {
                    Port = port
                }));
                return(WindowsServiceInstaller.Install(false, new string[0]));
            }

            var host = new ServiceHost(typeof(ServiceManager));

            host.AddServiceEndpoint(typeof(IServiceManager), new NetTcpBinding("Default"), "net.tcp://localhost:" + port + "/ServiceManager");
            try
            {
                host.Open();
            }
            catch
            {
                return(false);
            }
            _serviceHost = host;
            return(true);
        }
コード例 #3
0
        public Task <int> TryExecute(string[] arguments)
        {
            var installer = new WindowsServiceInstaller(_Configuration)
            {
                Context = _InstallContextProvider.GetContext()
            };

            var state = new Hashtable {
                [StateConstants.InstalledServiceName] = _Configuration.ServiceName
            };

            try
            {
                installer.Install(state);
                installer.Commit(state);

                _PersistentInstallState.Save(state);
            }
            catch
            {
                try
                {
                    installer.Rollback(state);
                }
                catch
                {
                    // Do nothing here
                }

                throw;
            }

            return(Task.FromResult(0));
        }
コード例 #4
0
        public void Can_Install_And_Uninstall_Service_Test()
        {
            var serviceName = "EmptyTestService";
            var installer = new WindowsServiceInstaller();

            var installResult = installer.Install(serviceName);

            Assert.IsTrue(installResult, $"cant install windows service: {serviceName}");

            var uninstallResult = installer.Uninstall(serviceName);

            Assert.IsTrue(uninstallResult, $"can't uninstall existing service: {serviceName}");
        }
コード例 #5
0
ファイル: Application.cs プロジェクト: luoyiminga/ServerX
        public bool UninstallService()
        {
            //if(IsWindowsServiceRunning)
            try
            {
                return(WindowsServiceInstaller.Install(true, new string[0]));
            }
            catch
            {
                return(false);
            }

            //if(_serviceHost != null)
            //{
            //    _serviceHost.Close();
            //    _serviceHost = null;
            //    return true;
            //}
            //return false;
        }
コード例 #6
0
ファイル: Runtime.cs プロジェクト: danryd/Tarro
 private static void ManageInstallation(string[] args)
 {
     if (args[0] == "-?")
     {
         PrintUsage();
     }
     var name = args[1];
     var installer = new WindowsServiceInstaller(name, ServiceAccount.NetworkService);
     if (args[0] == "install")
     {
         installer.Install(new Hashtable());
     }
     else if (args[0] == "uninstall")
     {
         installer.Uninstall(new Hashtable());
     }
     else
     {
         PrintUsage();
     }
 }