public void SetStartupType(string displayName, ServiceStartupType startupType)
        {
            try
            {
                //const string registryKey = @"SYSTEM\CurrentControlSet\services\{0}";
                const string registryKey = @"SYSTEM\CurrentControlSet\services\{0}";
                using (var key = Registry.LocalMachine.OpenSubKey(string.Format(registryKey, displayName), true))
                {
                    if (key == null)
                    {
                        throw new KeyNotFoundException($"サブキー 'HKEY_LOCAL_MACHINE\\{registryKey}' が存在しません。");
                    }

                    switch (startupType)
                    {
                    case ServiceStartupType.Disabled:
                        key.SetValue("Start", 4, RegistryValueKind.DWord);
                        return;

                    case ServiceStartupType.Manual:
                        key.SetValue("Start", 3, RegistryValueKind.DWord);
                        return;

                    case ServiceStartupType.Automatic:
                        key.SetValue("Start", 2, RegistryValueKind.DWord);
                        key.SetValue("DelayedAutostart", 0, RegistryValueKind.DWord);
                        return;

                    case ServiceStartupType.DelayStart:
                        key.SetValue("Start", 2, RegistryValueKind.DWord);
                        key.SetValue("DelayedAutostart", 1, RegistryValueKind.DWord);
                        return;
                    }
                }
            }
            catch (Exception e)
            {
                this._LogService.Error(e.Message);
                throw;
            }
        }
        public static bool SetServiceStartupType(ServiceStartupType startupType, string machineName = "")
        {
            try
            {
                var svcHandle = GetServiceHandle(machineName, Constants.FabricHostServiceName, null);
                if (svcHandle == IntPtr.Zero)
                {
                    throw new InvalidOperationException(string.Format(StringResources.Error_SvcConfigChangeNullSvcHandle, "SetStartupType"));
                }

                bool success = NativeMethods.ChangeServiceConfig(
                    svcHandle,
                    NativeMethods.SERVICE_NO_CHANGE,
                    (uint)startupType,
                    NativeMethods.SERVICE_NO_CHANGE,
                    null,
                    null,
                    IntPtr.Zero,
                    null,
                    null,
                    null,
                    null);

                if (!success)
                {
                    throw new Exception(
                              string.Format(StringResources.Error_UpdateServiceConfigNativeCall,
                                            "to auto",
                                            Marshal.GetLastWin32Error()));
                }

                return(true);
            }
            catch (Exception e)
            {
                DeployerTrace.WriteError(StringResources.Error_UnableToSetFabricHostServiceStartupType, startupType, e);
                return(false);
            }
        }
Exemple #3
0
 private void SetRegistryStarupType(ServiceStartupType value)
     => RegistryKey.SetValue(REGISTRY_STARTUP_KEYNAME, (int)value);
Exemple #4
0
        public ServiceStatus(IBitnamiRedmineService bitnamiRedmineService, string serviceName, ServiceStartupType startupType)
        {
            if (bitnamiRedmineService == null)
            {
                throw new ArgumentNullException(nameof(bitnamiRedmineService));
            }

            this._BitnamiRedmineService = bitnamiRedmineService;
            this._ServiceName           = serviceName;
            this._StartupType           = startupType;
        }
Exemple #5
0
        public WindowsService UpdatedStartupType(WindowsMachine machine, WindowsService service, ServiceStartupType serviceStartupType)
        {
            var objPath  = string.Format("select * from Win32_Service where DisplayName='{0}'", service.Name);
            var scope    = ConnectToMachine(machine);
            var observer = new ManagementOperationObserver();

            var searcher = new ManagementObjectSearcher(scope, new ObjectQuery(string.Format(objPath)));

            _isOperationCompleted = false;
            observer.ObjectReady += ObjectTobeReturned;


            //observer.Completed += Completed;
            //observer.Progress += Progress;
            foreach (ManagementObject mo in searcher.Get())
            {
                ManagementBaseObject inParams = mo.GetMethodParameters("ChangeStartMode");
                inParams["StartMode"] = StartTypeMapping.First(x => x.Value.Equals(serviceStartupType));

                mo.InvokeMethod(observer, "ChangeStartMode", inParams, null);
            }


            var sleepCount = 0;

            while (!_isOperationCompleted && sleepCount <= 60)
            {
                Thread.Sleep(1000);
                sleepCount++;
            }
            if (_isOperationCompleted)
            {
                service.StartUpType = serviceStartupType;
            }
            return(service);
        }
        public static bool ChangeServiceStartType(string ComputerName, string serviceName, ServiceStartupType startType)
        {
            try
            {
                IntPtr scmHandle = OpenSCManager(ComputerName, null, Pinvoke.SC_MANAGER_CONNECT);
                if (scmHandle == IntPtr.Zero)
                {
                    throw new Exception("[-] Failed to obtain a handle to the service control manager database.");
                }

                IntPtr serviceHandle = OpenService(scmHandle, serviceName, SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);
                if (serviceHandle == IntPtr.Zero)
                {
                    throw new Exception($"[-] Failed to obtain a handle to service '{serviceName}'.");
                }

                bool changeServiceSuccess = ChangeServiceConfig(serviceHandle, SERVICE_NO_CHANGE, (uint)startType, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null);
                if (!changeServiceSuccess)
                {
                    string msg = $"[-] Failed to update service configuration for service '{serviceName}'. ChangeServiceConfig returned error {Marshal.GetLastWin32Error()}.";
                    throw new Exception(msg);
                }

                if (scmHandle != IntPtr.Zero)
                {
                    CloseServiceHandle(scmHandle);
                }
                if (serviceHandle != IntPtr.Zero)
                {
                    CloseServiceHandle(serviceHandle);
                }
                Console.WriteLine("[+] Successfully updated RemoteRegistry service start type on host " + ComputerName);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine($"[-] Error: {e.Message}");
                return(false);
            }
        }
        private static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType <OnlineMode>(new ContainerControlledLifetimeManager(), new InjectionFactory((c) => { return(OnlineMode.Online); }));

            container.RegisterInstance(new ServiceStartupType(OnlineMode.Online), new ContainerControlledLifetimeManager());
            container.RegisterServiceRequests();

            container.RegisterInstance <IEventAggregator>(new EventAggregator(), new ContainerControlledLifetimeManager());

            container.AddNewExtension <Interception>();
            container.RegisterType <Service>();
            container.RegisterType <DDManController>();
            container.Configure <Interception>()
            .SetInterceptorFor <DDManController>(new VirtualMethodInterceptor());

            container.RegisterType <UserService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <GroupService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());


            container.RegisterType <ChangeLogService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <MasterDataService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <RoleService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <UserSettingService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <ProjectService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <CriteriaService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <CriterionSpecificationService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <LabelClusterService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <FunktionClusterService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <AKVSpecificationService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <TicketService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <UserImportMapService>(
                new Interceptor <VirtualMethodInterceptor>(),
                new InterceptionBehavior(new LoggingBehavior(TraceEventType.Verbose)),
                new InterceptionBehavior <OperationContextScopeHandler>());

            container.RegisterType <IUserLocator, UserLocator>();
            container.Configure <Interception>()
            .SetInterceptorFor <UserLocator>(new VirtualMethodInterceptor());

            container.RegisterType <Domain.UserModule.Module>();
            container.Resolve <Domain.UserModule.Module>().Initialize();

            container.RegisterType <Domain.SoftwareManagement.Module>();
            container.Resolve <Domain.SoftwareManagement.Module>().Initialize();

            container.RegisterType <Domain.Repository.Module>();
            container.Resolve <Domain.Repository.Module>().Initialize();

            container.RegisterType <Domain.MasterDataModule.Module>();
            container.Resolve <Domain.MasterDataModule.Module>().Initialize();

            container.RegisterType <Domain.Cluster.Module>();
            container.Resolve <Domain.Cluster.Module>().Initialize();

            container.RegisterType <Domain.TicketModule.Module>();
            container.Resolve <Domain.TicketModule.Module>().Initialize();

            container.RegisterType <Domain.PinboardModule.Module>();
            container.Resolve <Domain.PinboardModule.Module>().Initialize();

            container.RegisterType <Domain.QgroupModule.Module>();
            container.Resolve <Domain.QgroupModule.Module>().Initialize();

            container.RegisterType(typeof(EntityChangeMonitor <>));

            container.RegisterType <OpenItemEventDispatcher>(new InjectionConstructor(
                                                                 new ResolvedParameter <IEventAggregator>(),
                                                                 new ResolvedParameter <GenericDomainMapper <IUser, UserDto> >()));
            container.Configure <Interception>()
            .SetInterceptorFor <OpenItemEventDispatcher>(new VirtualMethodInterceptor());

            //If started from client these classes are not needed
            ServiceStartupType containerStartupType = container.Resolve <ServiceStartupType>();

            if (containerStartupType.OnlineMode == OnlineMode.Online)
            {
                container.RegisterInstance(container.Resolve <OpenItemEventDispatcher>(), new ContainerControlledLifetimeManager());
            }
        }