/// <summary> /// Build message handler delegate that will invoke all async + sync message handlers that were resolved from the container. /// </summary> /// <returns>Message handler delegate that invokes all async + sync message handlers that were resolved from the container.</returns> private MessageHandlerDelegate buildEventHandlerDelegate <TEvent>() where TEvent : class { var handlerDelegates = new List <MessageHandlerDelegate>(); try { // Get all async handlers for the event. IEnumerable <IEventAsyncHandler <TEvent> > resolvedHandlers = _containerAdapter.ResolveMultiple <IEventAsyncHandler <TEvent> >(); if (resolvedHandlers != null) { handlerDelegates.Add(EventHandlerDelegateBuilder.FromEventHandlers(resolvedHandlers)); } } catch (Exception ex) { // Some containers may throw exception when no instance is resolved. // This is to let clients have a way to be notified of the exception. _exceptionHandler?.Invoke(ex); } try { // Get all sync handlers for the event. IEnumerable <IEventHandler <TEvent> > syncEventHandlers = _containerAdapter.ResolveMultiple <IEventHandler <TEvent> >(); if (syncEventHandlers != null) { handlerDelegates.Add(EventHandlerDelegateBuilder.FromEventHandlers(syncEventHandlers, yieldExecution: _yieldExecutionOfSyncHandlers)); } } catch (Exception ex) { // Some containers may throw exception when no instance is resolved. // This is to let clients have a way to be notified of the exception. _exceptionHandler?.Invoke(ex); } // Instantiate a MessageHandlerDelegate. return((message, cancellationToken) => { // Task list. Task[] handleTasks = new Task[handlerDelegates.Count]; // Invoke each message handler delegates to start the tasks and add to task list. for (int i = 0; i < handlerDelegates.Count; i++) { handleTasks[i] = handlerDelegates[i].Invoke(message, cancellationToken); } // Wait for all tasks to complete. return Task.WhenAll(handleTasks); }); }
private IEnumerable <EventHandlerDelegate> buildEventHandlerDelegates <TEvent>() where TEvent : class, IEvent { List <EventHandlerDelegate> handlerDelegates = new List <EventHandlerDelegate>(); try { // Get all async handlers for the event. IEnumerable <IEventAsyncHandler <TEvent> > asyncEventHandlers = _containerAdapter.ResolveMultiple <IEventAsyncHandler <TEvent> >(); if (asyncEventHandlers != null) { // Convert to EventHandlerDelegate. handlerDelegates.AddRange(asyncEventHandlers.Select(eventHandler => { return(EventHandlerDelegateBuilder.FromEventHandler(eventHandler)); })); } } catch (Exception ex) { // Some containers may throw exception when no instance is resolved. _exceptionHandler?.Invoke(ex); } try { // Get all sync handlers for the event. IEnumerable <IEventHandler <TEvent> > syncEventHandlers = _containerAdapter.ResolveMultiple <IEventHandler <TEvent> >(); if (syncEventHandlers != null) { // Convert to EventHandlerDelegate. handlerDelegates.AddRange(syncEventHandlers.Select(eventHandler => { return(EventHandlerDelegateBuilder.FromEventHandler(eventHandler)); })); } } catch (Exception ex) { // Some containers may throw exception when no instance is resolved. _exceptionHandler?.Invoke(ex); } return(new ReadOnlyCollection <EventHandlerDelegate>(handlerDelegates)); }