private async Task <int> Run(string[] args) { Console.WriteLine(_programTitle); Console.WriteLine(); IOption option = Option.Build(args); if (option.Help) { option.FormatHelp() .ForEach(x => Console.WriteLine(x)); return(_ok); } CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); using (ILifetimeScope container = CreateContainer(option).BeginLifetimeScope(_lifetimeScopeTag)) { ITelemetry logger = container.Resolve <ITelemetry>(); IWorkContext context = new WorkContextBuilder() .Set(cancellationTokenSource.Token) .Set(logger) .Set(new ServiceProviderProxySimple(x => container.Resolve(x))) .Build(); option .FormatSettings() .ForEach(x => context.Telemetry.Info(context, x)); List <Task> runningTasks = new IAction?[] { option.Send?container.Resolve <SendEvents>() : null, option.Receive ? container.Resolve <ReceiveEvents>() : null, } .Where(x => x != null) .Select(x => Task.Run(() => x !.Run(context))) .ToList(); Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; cancellationTokenSource.Cancel(); Console.WriteLine("Canceling..."); }; Console.WriteLine("Hit Ctrl C to quite"); Console.WriteLine(); await Task.WhenAll(runningTasks); return(_ok); } }
private async Task <int> Run(string[] args) { Console.WriteLine(_programTitle); Console.WriteLine(); IOption option = Option.Build(args); if (option.Help) { option.FormatHelp() .ForEach(x => Console.WriteLine(option.SecretManager.Mask(x))); return(_ok); } CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); using (ILifetimeScope container = CreateContainer(option).BeginLifetimeScope(_lifetimeScopeTag)) { ITelemetry logger = container.Resolve <ITelemetry>(); IWorkContext context = new WorkContextBuilder() .Set(cancellationTokenSource.Token) .Set(logger) .Set(new ServiceProviderProxySimple(x => container.Resolve(x))) .Build(); option .FormatSettings() .ForEach(x => context.Telemetry.Info(context, x)); Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => { e.Cancel = true; cancellationTokenSource.Cancel(); Console.WriteLine("Canceling..."); }; Console.WriteLine("Hit Ctrl C to quit"); Console.WriteLine(); IExecutionContext executionContext = new ExecutionContext(); await new IAction[] { container.Resolve <LoadAssemblyAction>(), } .ForEachAsync(x => x.Run(context, executionContext)); return(_ok); } }