private Task InvokeHandlersInternal(IEnumerable <object> handlers, object message)
        {
            if (handlers == null)
            {
                throw new ArgumentNullException("message");
            }

            var tasks = new List <Task>();

            var asList = handlers.ToList();

            asList.Aggregate(tasks, (c, handler) =>
            {
                var del     = HandlerDelegateFactory.GetdMessageHandlerDelegate(handler.GetType(), message.GetType());
                var delTask = del(handler, new[] { message });
                tasks.Add(delTask);
                return(c);
            });

            var whenAllTask = Task.WhenAll(tasks)
                              .ContinueWith(t =>
            {
                foreach (var h in asList)
                {
                    var disposable = h as IDisposable;
                    disposable?.Dispose();
                }

                if (t.Exception != null)
                {
                    throw t.Exception;
                }
            });

            return(whenAllTask);
        }
Exemple #2
0
 public static Func <object, object[], Task> GetdMessageHandlerDelegate(Type handlerType, Type commandType)
 {
     return(HandlerDelegateFactory.MessageHandlerDelegatesCache.GetOrAdd(new Tuple <Type, Type>(handlerType, commandType), t => HandlerDelegateFactory.BuildMessageHandlerDelegateInternal(handlerType, commandType)));
 }