private void GivenServiceControlManagerIsExpectingService()
        {
            A.CallTo(() => nativeInterop.StartServiceCtrlDispatcherW(A <ServiceTableEntry[]> ._))
            .Invokes(new Action <ServiceTableEntry[]>(HandleNativeStartServiceCtrlDispatcherW))
            .Returns(value: true);
            A.CallTo(() => nativeInterop.RegisterServiceCtrlHandlerExW(TestServiceName, A <ServiceControlHandler> ._, A <IntPtr> ._))
            .ReturnsLazily((string serviceName, ServiceControlHandler controlHandler, IntPtr context) =>
            {
                serviceName.Should().Be(TestServiceName);
                serviceControlHandler = controlHandler;
                serviceControlContext = context;

                return(serviceStatusHandle);
            });
        }
        private void ServiceMainFunction(int numArgs, IntPtr argPtrPtr)
        {
            var startupArguments = ParseArguments(numArgs, argPtrPtr);

            serviceStatusHandle = nativeInterop.RegisterServiceCtrlHandlerExW(serviceName, serviceControlHandlerDelegate, IntPtr.Zero);

            if (serviceStatusHandle.IsInvalid)
            {
                stopTaskCompletionSource.SetException(new Win32Exception(Marshal.GetLastWin32Error()));
                return;
            }

            ReportServiceStatus(ServiceState.StartPening, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 3000);

            try
            {
                stateMachine.OnStart(startupArguments, ReportServiceStatus);
            }
            catch
            {
                ReportServiceStatus(ServiceState.Stopped, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: -1, waitHint: 0);
            }
        }