public async Task Invoke(IDictionary <string, object> environment) { _controller.OnStop += async() => { try { await Task.Run(() => { var exitCode = _service.Stop(); _service.OnUnhandledException -= OnUnhandledException; _controller.Stopped(exitCode); }); } catch (Exception exception) { OnUnhandledException(exception); } }; try { await Task.Run(() => { var startParameters = environment[Owin.StartParameters] as string[] ?? new string[0]; _service.OnUnhandledException += OnUnhandledException; var startCode = _service.Start(startParameters); _controller.Started(startCode); }); } catch (Exception exception) { OnUnhandledException(exception); } if (_next != null) { await _next.Invoke(environment); } }
public static void Run(string[] args, IWindowsService service) { var serviceName = service.GetType().Name; service.WriteToConsole += OnWriteToConsole; var isRunning = true; AllocConsole(); service.Start(args); while (isRunning) { WriteToConsole(ConsoleColor.Yellow, "Enter either [Q]uit, [P]ause, [R]esume : "); isRunning = HandleConsoleInput(service, Console.ReadLine()); } service.Stop(); service.Shutdown(); }