public ServiceHostController Start()
        {
            var service = new ServiceController(_configuration.GetInstancedServiceName());

            if (service.Status != ServiceControllerStatus.Running)
            {
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromMilliseconds(_configuration.Timeout));
            }

            return(this);
        }
        public void Install(IServiceConfiguration configuration)
        {
            Guard.AgainstNull(configuration, nameof(configuration));

            GuardAdministrator();

            if (!string.IsNullOrEmpty(configuration.ServicePath))
            {
                new ServiceInvoker(configuration).Execute(ServiceCommand.Install);

                return;
            }

            CallContext.LogicalSetData(ServiceConfigurationKey, configuration);

            var instancedServiceName = configuration.GetInstancedServiceName();
            var log = ServiceHostEventLog.GetEventLog(instancedServiceName);

            ConsoleExtensions.WriteLine(ConsoleColor.Green, $"Installing service '{instancedServiceName}'.");

            var entryAssembly = Assembly.GetEntryAssembly();

            if (entryAssembly == null)
            {
                throw new InvalidOperationException("An entry assembly is required in order to install a service.");
            }

            var entryAssemblyLocation = entryAssembly.Location;

            if (string.IsNullOrEmpty(entryAssemblyLocation))
            {
                throw new InvalidOperationException("The entry assembly has no location.");
            }

            if (!Path.GetExtension(entryAssemblyLocation).Equals(".exe", StringComparison.InvariantCultureIgnoreCase))
            {
                throw new InvalidOperationException(
                          "The entry assembly must be an 'exe' in order to install as a service.");
            }

            var assemblyInstaller = new AssemblyInstaller(typeof(ServiceHost).Assembly, null);

            using (var installer = assemblyInstaller)
            {
                IDictionary state = new Hashtable();

                installer.UseNewContext = true;

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

                    var serviceKey = GetServiceKey(instancedServiceName);

                    serviceKey.SetValue("Description", configuration.Description);
                    serviceKey.SetValue("ImagePath",
                                        $"{entryAssemblyLocation} /serviceName=\"{configuration.ServiceName}\"{(string.IsNullOrEmpty(configuration.Instance) ? string.Empty : $" /instance=\"{configuration.Instance}\"")}");
                }