Exemplo n.º 1
0
        public ServiceHandle CreateService(string serviceName
                                           , string displayName
                                           , string binaryPath
                                           , ServiceType serviceType
                                           , ServiceStartType startupType
                                           , ErrorSeverity errorSeverity
                                           , Win32ServiceCredentials credentials)
        {
            ServiceHandle service = NativeInterop.CreateServiceW(this, serviceName, displayName, ServiceControlAccessRights.All, serviceType, startupType, errorSeverity,
                                                                 binaryPath, null,
                                                                 System.IntPtr.Zero, null, credentials.UserName, credentials.Password);

            service.NativeInterop = NativeInterop;

            if (service.IsInvalid)
            {
                throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
            }

            return(service);
        }
Exemplo n.º 2
0
        public virtual void ChangeConfig(string displayName, string binaryPath, ServiceType serviceType, ServiceStartType startupType, ErrorSeverity errorSeverity, Win32ServiceCredentials credentials)
        {
            var success = NativeInterop.ChangeServiceConfigW(this, serviceType, startupType, errorSeverity, binaryPath, null, IntPtr.Zero, null, credentials.UserName, credentials.Password, displayName);

            if (!success)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }
        }
Exemplo n.º 3
0
 public void CreateService(string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials,
                           bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal)
 {
     CreateService(
         new ServiceDefinitionBuilder(serviceName)
         .WithDisplayName(displayName)
         .WithDescription(description)
         .WithBinaryPath(binaryPath)
         .WithCredentials(credentials)
         .WithAutoStart(autoStart)
         .WithErrorSeverity(errorSeverity)
         .Build(),
         startImmediately
         );
 }
Exemplo n.º 4
0
        public void CreateOrUpdateService(string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, ServiceFailureActions serviceFailureActions, bool failureActionsOnNonCrashFailures, bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal)
        {
            if (string.IsNullOrEmpty(binaryPath))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(binaryPath));
            }
            if (string.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(serviceName));
            }

            try
            {
                using (var mgr = ServiceControlManager.Connect(nativeInterop, machineName, databaseName, ServiceControlManagerAccessRights.All))
                {
                    ServiceHandle  existingService;
                    Win32Exception errorException;
                    if (mgr.TryOpenService(serviceName, ServiceControlAccessRights.All, out existingService, out errorException))
                    {
                        using (existingService)
                        {
                            DoUpdateService(displayName, description, binaryPath, credentials, autoStart, errorSeverity, existingService, serviceFailureActions, failureActionsOnNonCrashFailures);
                        }
                    }
                    else
                    {
                        if (errorException.NativeErrorCode == KnownWin32ErrorCoes.ERROR_SERVICE_DOES_NOT_EXIST)
                        {
                            DoCreateService(mgr, serviceName, displayName, description, binaryPath, credentials, autoStart, startImmediately, errorSeverity, serviceFailureActions, failureActionsOnNonCrashFailures);
                        }
                        else
                        {
                            throw errorException;
                        }
                    }
                }
            }
            catch (DllNotFoundException dllException)
            {
                throw new PlatformNotSupportedException(nameof(Win32ServiceHost) + " is only supported on Windows with service management API set.", dllException);
            }
        }
Exemplo n.º 5
0
 public void CreateOrUpdateService(string serviceName, string displayName, string description, string binaryPath,
                                   Win32ServiceCredentials credentials, bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal)
 {
     CreateOrUpdateService(serviceName, displayName, description, binaryPath, credentials, null, false, autoStart, startImmediately, errorSeverity);
 }
Exemplo n.º 6
0
        private void DoCreateService(ServiceControlManager serviceControlManager, string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, bool autoStart, bool startImmediately, ErrorSeverity errorSeverity, ServiceFailureActions serviceFailureActions, bool failureActionsOnNonCrashFailures)
        {
            using (var svc = serviceControlManager.CreateService(serviceName, displayName, binaryPath, ServiceType.Win32OwnProcess,
                                                                 autoStart ? ServiceStartType.AutoStart : ServiceStartType.StartOnDemand, errorSeverity, credentials))
            {
                if (!string.IsNullOrEmpty(description))
                {
                    svc.SetDescription(description);
                }

                if (serviceFailureActions != null)
                {
                    svc.SetFailureActions(serviceFailureActions);
                    svc.SetFailureActionFlag(failureActionsOnNonCrashFailures);
                }

                if (startImmediately)
                {
                    svc.Start();
                }
            }
        }
Exemplo n.º 7
0
        public void CreateService(string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, ServiceFailureActions serviceFailureActions, bool failureActionsOnNonCrashFailures, bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal)
        {
            if (string.IsNullOrEmpty(binaryPath))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(binaryPath));
            }
            if (string.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(serviceName));
            }

            try
            {
                using (var mgr = ServiceControlManager.Connect(nativeInterop, machineName, databaseName, ServiceControlManagerAccessRights.All))
                {
                    DoCreateService(mgr, serviceName, displayName, description, binaryPath, credentials, autoStart, startImmediately, errorSeverity, serviceFailureActions, failureActionsOnNonCrashFailures);
                }
            }
            catch (DllNotFoundException dllException)
            {
                throw new PlatformNotSupportedException(nameof(Win32ServiceHost) + " is only supported on Windows with service management API set.", dllException);
            }
        }
Exemplo n.º 8
0
 private static void DoUpdateService(string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, bool autoStart, ErrorSeverity errorSeverity, ServiceHandle existingService, ServiceFailureActions serviceFailureActions, bool failureActionsOnNonCrashFailures)
 {
     existingService.ChangeConfig(displayName, binaryPath, ServiceType.Win32OwnProcess,
                                  autoStart ? ServiceStartType.AutoStart : ServiceStartType.StartOnDemand, errorSeverity, credentials);
     existingService.SetDescription(description);
     if (serviceFailureActions != null)
     {
         existingService.SetFailureActions(serviceFailureActions);
         existingService.SetFailureActionFlag(failureActionsOnNonCrashFailures);
     }
     existingService.Start(throwIfAlreadyRunning: false);
 }
Exemplo n.º 9
0
        public void CreateService(string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal)
        {
            if (string.IsNullOrEmpty(binaryPath))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(binaryPath));
            }
            if (string.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(serviceName));
            }
            if (string.IsNullOrEmpty(binaryPath))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(binaryPath));
            }

            try
            {
                using (var mgr = ServiceControlManager.Connect(nativeInterop, machineName, databaseName, ServiceControlManagerAccessRights.All))
                {
                    using (
                        var svc = mgr.CreateService(serviceName, displayName, binaryPath, ServiceType.Win32OwnProcess,
                                                    autoStart ? ServiceStartType.AutoStart : ServiceStartType.StartOnDemand, errorSeverity, credentials))
                    {
                        if (!string.IsNullOrEmpty(description))
                        {
                            svc.SetDescription(description);
                        }
                        if (startImmediately)
                        {
                            svc.Start();
                        }
                    }
                }
            }
            catch (DllNotFoundException dllException)
            {
                throw new PlatformNotSupportedException(nameof(Win32ServiceHost) + " is only supported on Windows with service management API set.", dllException);
            }
        }
Exemplo n.º 10
0
 public ServiceDefinitionBuilder WithCredentials(Win32ServiceCredentials credentials)
 {
     Credentials = credentials;
     return(this);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Creates or update a service in windows
        /// </summary>
        /// <param name="serviceName">Service name</param>
        /// <param name="displayName">Display name</param>
        /// <param name="description">Descriptions</param>
        /// <param name="binaryPath">Binary path</param>
        /// <param name="credentials">Credentials</param>
        /// <param name="serviceFailureActions">Service failure actions</param>
        /// <param name="failureActionsOnNonCrashFailures">Failure actions on non crash failures</param>
        /// <param name="autoStart">Sets the service to autostart</param>
        /// <param name="startImmediately">Starts inmediately after creation</param>
        /// <param name="errorSeverity">Error severity</param>
        public void CreateOrUpdateService(string serviceName, string displayName, string description, string binaryPath, Win32ServiceCredentials credentials, ServiceFailureActions serviceFailureActions, bool failureActionsOnNonCrashFailures, bool autoStart = false, bool startImmediately = false, ErrorSeverity errorSeverity = ErrorSeverity.Normal)
        {
            if (string.IsNullOrEmpty(binaryPath))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(binaryPath));
            }
            if (string.IsNullOrEmpty(serviceName))
            {
                throw new ArgumentException("Value cannot be null or empty.", nameof(serviceName));
            }

            try
            {
                using (var mgr = ServiceControlManager.Connect(_nativeInterop, _machineName, _databaseName, ServiceControlManagerAccessRights.All))
                {
                    if (mgr.TryOpenService(serviceName, ServiceControlAccessRights.All, out var existingService, out var errorException))
                    {
                        using (existingService)
                        {
                            DoUpdateService(displayName, description, binaryPath, credentials, autoStart, errorSeverity, existingService, serviceFailureActions, failureActionsOnNonCrashFailures);
                        }
                    }
                    else
                    {
                        if (errorException.NativeErrorCode == KnownWin32ErrorCoes.ERROR_SERVICE_DOES_NOT_EXIST)
                        {
                            DoCreateService(mgr, serviceName, displayName, description, binaryPath, credentials, autoStart, startImmediately, errorSeverity, serviceFailureActions, failureActionsOnNonCrashFailures);
                        }
                        else
                        {
                            throw errorException;
                        }
                    }
                }