Exemplo n.º 1
0
        /// <summary>
        /// Runs the windows service that this instance was initialized with.
        /// This method is inteded to be run from the application's main thread and will block until the service has stopped.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="Win32Exception">Thrown when an exception ocurrs when communicating with windows' service system.</exception>
        /// <exception cref="PlatformNotSupportedException">Thrown when run on a non-windows platform.</exception>
        public static int RunAsService(Options opt)
        {
            ServiceOptions = opt;
            serviceMainFunctionDelegate   = MainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
            var serviceTable = new ServiceTableEntry[2]; // second one is null/null to indicate termination

            serviceTable[0].serviceName         = ServiceName;
            serviceTable[0].serviceMainFunction = Marshal.GetFunctionPointerForDelegate(serviceMainFunctionDelegate);

            try
            {
                // StartServiceCtrlDispatcherW call returns when ServiceMainFunction has exited and all services have stopped
                // at least this is what's documented even though linked c++ sample has an additional stop event
                // to let the service main function dispatched to block until the service stops.
                if (!Win32ServiceInterop.Wrapper.StartServiceCtrlDispatcherW(serviceTable))
                {
                    opt.LogError("ASTOOL running as Window Service - Error while calling StartServiceCtrlDispatcherW");
                }
            }
            catch (DllNotFoundException dllException)
            {
                opt.LogError("ASTOOL running as Window Service - this service is not available on the current platform: " + dllException.Message);
            }

            if (resultException != null)
            {
                opt.LogError("ASTOOL running as Window Service - Exception: " + resultException.Message);
            }

            return(resultCode);
        }
Exemplo n.º 2
0
 internal Win32ServiceHost(string serviceName, IWin32ServiceStateMachine stateMachine, INativeInterop nativeInterop)
 {
     _serviceName   = serviceName ?? throw new ArgumentNullException(nameof(serviceName));
     _stateMachine  = stateMachine ?? throw new ArgumentNullException(nameof(stateMachine));
     _nativeInterop = nativeInterop ?? throw new ArgumentNullException(nameof(nativeInterop));
     _serviceMainFunctionDelegate   = ServiceMainFunction;
     _serviceControlHandlerDelegate = HandleServiceControlCommand;
 }
        internal Win32ServiceHost([NotNull] string serviceName, [NotNull] IWin32ServiceStateMachine stateMachine, [NotNull] INativeInterop nativeInterop)
        {
            this.serviceName   = serviceName ?? throw new System.ArgumentNullException(nameof(serviceName));
            this.stateMachine  = stateMachine ?? throw new System.ArgumentNullException(nameof(stateMachine));
            this.nativeInterop = nativeInterop ?? throw new System.ArgumentNullException(nameof(nativeInterop));

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
Exemplo n.º 4
0
        internal Win32ServiceHost([NotNull] IWin32Service service, [NotNull] INativeInterop nativeInterop)
        {
            serviceName        = service.ServiceName;
            this.stateMachine  = (service == null) ? throw new ArgumentNullException(nameof(service)) : new SimpleServiceStateMachine(service);
            this.nativeInterop = nativeInterop ?? throw new ArgumentNullException(nameof(nativeInterop));

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
        internal Win32ServiceHost([NotNull] IPausableWin32Service service, [NotNull] INativeInterop nativeInterop)
        {
            if (service == null)
            {
                throw new System.ArgumentNullException(nameof(service));
            }

            this.nativeInterop = nativeInterop ?? throw new System.ArgumentNullException(nameof(nativeInterop));
            serviceName        = service.ServiceName;
            stateMachine       = new PausableServiceStateMachine(service);

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
        internal Win32ServiceHost(IWin32Service service, INativeInterop nativeInterop)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (nativeInterop == null)
            {
                throw new ArgumentNullException(nameof(nativeInterop));
            }

            serviceName        = service.ServiceName;
            stateMachine       = new SimpleServiceStateMachine(service);
            this.nativeInterop = nativeInterop;

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }
        internal Win32ServiceHost(string serviceName, IWin32ServiceStateMachine stateMachine, INativeInterop nativeInterop)
        {
            if (serviceName == null)
            {
                throw new ArgumentNullException(nameof(serviceName));
            }
            if (stateMachine == null)
            {
                throw new ArgumentNullException(nameof(stateMachine));
            }
            if (nativeInterop == null)
            {
                throw new ArgumentNullException(nameof(nativeInterop));
            }

            this.serviceName   = serviceName;
            this.stateMachine  = stateMachine;
            this.nativeInterop = nativeInterop;

            serviceMainFunctionDelegate   = ServiceMainFunction;
            serviceControlHandlerDelegate = HandleServiceControlCommand;
        }