예제 #1
0
        private void ReportServiceStatus(ServiceState state, ServiceAcceptedControlCommandsFlags acceptedControlCommands, int win32ExitCode, uint waitHint)
        {
            // Recuso deixar ou alterar o estado final
            if (serviceStatus.State == ServiceState.Stopped)
            {
                return;
            }

            serviceStatus.State         = state;
            serviceStatus.Win32ExitCode = win32ExitCode;
            serviceStatus.WaitHint      = waitHint;

            serviceStatus.AcceptedControlCommands = state == ServiceState.Stopped
                ? ServiceAcceptedControlCommandsFlags.None // Uma vez que aplicamos o "Parado" como estado final, deixamos de aceitar as mensagens de controle.
                : acceptedControlCommands;

            serviceStatus.CheckPoint = state == ServiceState.Running ||
                                       state == ServiceState.Stopped ||
                                       state == ServiceState.Paused
                ? 0 // MSDN: Este valor não é válido e deve ser zero quando o serviço não tiver início, parar, pausar ou continuar a operação pendente.
                : checkpointCounter++;

            nativeInterop.SetServiceStatus(serviceStatusHandle, ref serviceStatus);

            if (state == ServiceState.Stopped)
            {
                stopTaskCompletionSource.TrySetResult(win32ExitCode);
            }
        }
        private void ReportServiceStatus(ServiceState state, ServiceAcceptedControlCommandsFlags acceptedControlCommands, int win32ExitCode, uint waitHint)
        {
            if (serviceStatus.State == ServiceState.Stopped)
            {
                // we refuse to leave or alter the final state
                return;
            }

            serviceStatus.State         = state;
            serviceStatus.Win32ExitCode = win32ExitCode;
            serviceStatus.WaitHint      = waitHint;

            serviceStatus.AcceptedControlCommands = state == ServiceState.Stopped
                ? ServiceAcceptedControlCommandsFlags.None // since we enforce "Stopped" as final state, no longer accept control messages
                : acceptedControlCommands;

            serviceStatus.CheckPoint = state == ServiceState.Running || state == ServiceState.Stopped || state == ServiceState.Paused
                ? 0 // MSDN: This value is not valid and should be zero when the service does not have a start, stop, pause, or continue operation pending.
                : checkpointCounter++;

            nativeInterop.SetServiceStatus(serviceStatusHandle, ref serviceStatus);

            if (state == ServiceState.Stopped)
            {
                stopTaskCompletionSource.TrySetResult(win32ExitCode);
            }
        }
예제 #3
0
        public ServiceLifecycleTests()
        {
            A.CallTo(() => serviceStateMachine.OnStart(A <string[]> ._, A <ServiceStatusReportCallback> ._))
            .Invokes((string[] args, ServiceStatusReportCallback callback) =>
            {
                statusReportCallback = callback;
            });

            var dummy = new ServiceStatus();

            A.CallTo(() => nativeInterop.SetServiceStatus(null, ref dummy))
            .WithAnyArguments()
            .Returns(value: true)
            .AssignsOutAndRefParametersLazily((ServiceStatusHandle handle, ServiceStatus status) =>
            {
                if (handle == serviceStatusHandle)
                {
                    reportedServiceStatuses.Add(status);
                    if (status.State == ServiceState.Stopped)
                    {
                        serviceStoppedEvent.Set();
                    }
                }
                return(new object[] { status });
            });

            sut = new ServiceUtils.Win32ServiceHost(TestServiceName, serviceStateMachine, nativeInterop);
        }