private static async Task MainAsync(IAmACommandProcessor commandProcessor) { // To allow the event handler(s) to release the thread // while doing potentially long running operations, // await the async (but inline) handling of the events await commandProcessor.PublishAsync(new GreetingEvent("Ian")); try { // This will cause an exception in one event handler await commandProcessor.PublishAsync(new GreetingEvent("Roger")); } catch (AggregateException e) { Console.WriteLine("Aggregate exception thrown after event Roger:"); ShowExceptions(e, 1); } try { // This will cause an exception in both event handlers await commandProcessor.PublishAsync(new GreetingEvent("Devil")); } catch (AggregateException e) { Console.WriteLine("Aggregate exception thrown after event Devil:"); ShowExceptions(e, 1); } }
public async Task StartAsync(CancellationToken cancellationToken) { // To allow the event handler(s) to release the thread // while doing potentially long running operations, // await the async (but inline) handling of the events await _commandProcessor.PublishAsync(new GreetingEvent("Ian"), cancellationToken : cancellationToken); try { // This will cause an exception in one event handler await _commandProcessor.PublishAsync(new GreetingEvent("Roger"), cancellationToken : cancellationToken); } catch (AggregateException e) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Aggregate exception thrown after event Roger:"); ShowExceptions(e, 1); Console.ResetColor(); } try { // This will cause an exception in both event handlers await _commandProcessor.PublishAsync(new GreetingEvent("Devil"), cancellationToken : cancellationToken); } catch (AggregateException e) { Console.BackgroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Aggregate exception thrown after event Devil:"); ShowExceptions(e, 1); Console.ResetColor(); } }
public async Task PublishAsync <T>(T @event, bool continueOnCapturedContext = false, CancellationToken cancellationToken = default) where T : class, IRequest { await _commandProcessor.PublishAsync(@event, continueOnCapturedContext, cancellationToken); }
/// <summary> /// Publish an event 0-n handler functions. /// </summary> /// <param name="eventToPublish">Event object to be published on the event bus.</param> /// <param name="cancellationToken">Cancellation token</param> /// <returns>Task.</returns> Task IEventPublisher.Publish(IList <IEvent> eventToPublish, CancellationToken?cancellationToken) => Task.WhenAll( eventToPublish .Select(p => _commandProcessor.PublishAsync(new BrighterEvent(p))) .ToList() );