public bool Handle(HostArguments args, HandleAsWindowsService service)
            {
                if (_shouldHandleAsWindowsService)
                {
                    _consoleWriterQueue.Enqueue("WindowsService.Starting");

                    using (service.OnStartFactory())
                    {
                        // simulates that windows service is started
                        _consoleWriterQueue.Enqueue("WindowsService.Running");

                        // Block - simulates that windows service is running
                        Thread.Sleep(10);

                        // Simulates that Windows Service is shutting down
                        _consoleWriterQueue.Enqueue("WindowsService.Stopping");
                    }

                    _consoleWriterQueue.Enqueue("WindowsService.Stopped");

                    return(true);
                }

                _consoleWriterQueue.Enqueue("NotWindowsService");
                return(false);
            }
コード例 #2
0
        public void Intercept(IInvocation invocation)
        {
            if (_isHostHandleSpec.IsSatisfiedBy(invocation))
            {
                if (invocation.InvocationTarget is IHost host && invocation.Arguments.FirstOrDefault() is HostArguments arguments)
                {
                    var handle = new HandleAsWindowsService(host.Name(), host.Name(), host.Description, () =>
                    {
                        // Instantiate a WaitHandle to signal that we're running as a Windows Service
                        return(_waitHandle = new WindowsServiceWaitHandle(invocation.Proceed, _shutdown.Token, _logger));
                    });

                    // If we're not a windows service, then just go ahead and execute the Host-implementation.
                    if (!_windowsServiceHandler.Handle(arguments, handle))
                    {
                        invocation.Proceed();
                    }
                }
            }
            else if (_isWaitForShutdownRequestSpec.IsSatisfiedBy(invocation))
            {
                if (_waitHandle != null)
                {
                    // Wait for Windows Service to signal a shutdown/stop
                    _waitHandle.Wait();
                }
                else
                {
                    // Execute the underlying Wait()-method
                    invocation.Proceed();
                }
            }
            else
            {
                // Execute the underlying method
                invocation.Proceed();
            }
        }