コード例 #1
0
        private static Task InvokeActionAsyncCore(EventHandlerContext context, CancellationToken cancellationToken)
        {
            Contract.Requires(context != null);
            Contract.Requires(context.Descriptor != null);

            EventHandlerDescriptor handlerDescriptor = context.Descriptor;
            return handlerDescriptor.ExecuteAsync(context, cancellationToken);
        }
コード例 #2
0
            public ActionInvoker(EventHandlerContext context, CancellationToken cancellationToken, ServicesContainer services)
            {
                Contract.Requires(services != null);

                this.context = context;
                this.cancellationToken = cancellationToken;
                this.services = services;
            }
コード例 #3
0
        /// <inheritdocs />
        public virtual Task InvokeHandlerAsync(EventHandlerContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            return(InvokeActionAsyncCore(context, cancellationToken));
        }
コード例 #4
0
        private static Task InvokeActionAsyncCore(EventHandlerContext context, CancellationToken cancellationToken)
        {
            Contract.Requires(context != null);
            Contract.Requires(context.Descriptor != null);

            EventHandlerDescriptor handlerDescriptor = context.Descriptor;

            return(handlerDescriptor.ExecuteAsync(context, cancellationToken));
        }
コード例 #5
0
        /// <inheritdocs />
        public virtual Task InvokeHandlerAsync(EventHandlerContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            return InvokeActionAsyncCore(context, cancellationToken);
        }
コード例 #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="EventHandlerOccurredContext"/> class.
        /// </summary>
        /// <param name="handlerContext">Then handler context.</param>
        /// <param name="exceptionInfo">The <see cref="ExceptionDispatchInfo"/>. Optionnal.</param>
        public EventHandlerOccurredContext(EventHandlerContext handlerContext, ExceptionDispatchInfo exceptionInfo)
        {
            if (handlerContext == null)
            {
                throw Error.ArgumentNull("handlerContext");
            }

            this.handlerContext = handlerContext;
            this.ExceptionInfo = exceptionInfo;
        }
コード例 #7
0
        public EventHandlerFilterResult(EventHandlerContext context, ServicesContainer services, IEventHandlerFilter[] filters)
        {
            Contract.Requires(context != null);
            Contract.Requires(services != null);
            Contract.Requires(filters != null);

            this.context = context;
            this.services = services;
            this.filters = filters;
        }
コード例 #8
0
        public virtual Task OnEventOccurringAsync(EventHandlerContext eventOccurringContext, CancellationToken cancellationToken)
        {
            try
            {
                this.OnEventOccurring(eventOccurringContext);
            }
            catch (Exception ex)
            {
                return TaskHelpers.FromError(ex);
            }

            return TaskHelpers.Completed();
        }
コード例 #9
0
        public virtual Task OnEventOccurringAsync(EventHandlerContext eventOccurringContext, CancellationToken cancellationToken)
        {
            try
            {
                this.OnEventOccurring(eventOccurringContext);
            }
            catch (Exception ex)
            {
                return(TaskHelpers.FromError(ex));
            }

            return(TaskHelpers.Completed());
        }
コード例 #10
0
        /// <summary>
        /// Executes the filter handler asynchronously.
        /// </summary>
        /// <param name="handlerContext">The handler context.</param>
        /// <param name="cancellationToken">The cancellation token assigned for this task.</param>
        /// <param name="continuation">The delegate function to continue after the handler method is invoked.</param>
        /// <returns>The newly created task for this operation.</returns>
        public Task ExecuteHandlerFilterAsync(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func <Task> continuation)
        {
            if (handlerContext == null)
            {
                throw Error.ArgumentNull("handlerContext");
            }

            if (continuation == null)
            {
                throw Error.ArgumentNull("continuation");
            }

            return(this.ExecuteHandlerFilterAsyncCore(handlerContext, cancellationToken, continuation));
        }
コード例 #11
0
        public static Func<Task> InvokeHandlerWithHandlerFiltersAsync(EventHandlerContext context, CancellationToken cancellationToken, IEventHandlerFilter[] filters, Func<Task> innerAction)
        {
            Contract.Requires(context != null);
            Contract.Requires(filters != null);
            Contract.Requires(innerAction != null);

            Func<Task> result = innerAction;
            for (int i = filters.Length - 1; i >= 0; i--)
            {
                IEventHandlerFilter commandFilter = filters[i];
                Func<Func<Task>, IEventHandlerFilter, Func<Task>> chainContinuation = (continuation, innerFilter) => () => innerFilter.ExecuteHandlerFilterAsync(context, cancellationToken, continuation);

                result = chainContinuation(result, commandFilter);
            }

            return result;
        }
コード例 #12
0
        public virtual Task ExecuteAsync(EventHandlerContext context, CancellationToken cancellationToken)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (cancellationToken.IsCancellationRequested)
            {
                return(TaskHelpers.Canceled());
            }

            try
            {
                return(this.actionExecutor.Value.Execute(context));
            }
            catch (Exception e)
            {
                return(TaskHelpers.FromError(e));
            }
        }
コード例 #13
0
        private async Task CallOnHandlerOccurrededAsync(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func <Task> continuation)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ExceptionDispatchInfo exceptionInfo = null;

            try
            {
                await continuation();
            }
            catch (Exception e)
            {
                exceptionInfo = ExceptionDispatchInfo.Capture(e);
            }

            EventHandlerOccurredContext occurredContext = new EventHandlerOccurredContext(handlerContext, exceptionInfo);

            await this.OnEventOccurredAsync(occurredContext, cancellationToken);

            if (occurredContext.ExceptionInfo != null)
            {
                if (exceptionInfo == null)
                {
                    occurredContext.ExceptionInfo.Throw();
                }
                else
                {
                    Exception exception    = exceptionInfo.SourceException;
                    Exception newException = occurredContext.ExceptionInfo.SourceException;
                    if (newException == exception)
                    {
                        exceptionInfo.Throw();
                    }
                    else
                    {
                        occurredContext.ExceptionInfo.Throw();
                    }
                }
            }
        }
コード例 #14
0
ファイル: DefaultEventWorker.cs プロジェクト: ctguxp/Waffle
        private static Task InvokeHandlerAsync(EventHandlerDescriptor descriptor, EventHandlerRequest request, CancellationToken cancellationToken)
        {
            Contract.Requires(descriptor != null);
            Contract.Requires(request != null);

            IEventHandler handler = descriptor.CreateHandler(request);
            if (handler == null)
            {
                throw CreateHandlerNotFoundException(descriptor);
            }

            request.RegisterForDispose(handler, true);
            EventHandlerContext context = new EventHandlerContext(request, descriptor);
            context.Handler = handler;
            handler.EventContext = context;
            EventFilterGrouping filterGrouping = descriptor.GetFilterGrouping();
     
            ServicesContainer servicesContainer = request.Configuration.Services;
            IEventHandlerResult result = new EventHandlerFilterResult(context, servicesContainer, filterGrouping.EventHandlerFilters);

            return result.ExecuteAsync(cancellationToken);
        }
コード例 #15
0
        private static Task InvokeHandlerAsync(EventHandlerDescriptor descriptor, EventHandlerRequest request, CancellationToken cancellationToken)
        {
            Contract.Requires(descriptor != null);
            Contract.Requires(request != null);

            IEventHandler handler = descriptor.CreateHandler(request);

            if (handler == null)
            {
                throw CreateHandlerNotFoundException(descriptor);
            }

            request.RegisterForDispose(handler, true);
            EventHandlerContext context = new EventHandlerContext(request, descriptor);

            context.Handler      = handler;
            handler.EventContext = context;
            EventFilterGrouping filterGrouping = descriptor.GetFilterGrouping();

            ServicesContainer   servicesContainer = request.Configuration.Services;
            IEventHandlerResult result            = new EventHandlerFilterResult(context, servicesContainer, filterGrouping.EventHandlerFilters);

            return(result.ExecuteAsync(cancellationToken));
        }
コード例 #16
0
        /// <summary>
        /// Executes the filter handler asynchronously.
        /// </summary>
        /// <param name="handlerContext">The handler context.</param>
        /// <param name="cancellationToken">The cancellation token assigned for this task.</param>
        /// <param name="continuation">The delegate function to continue after the handler method is invoked.</param>
        /// <returns>The newly created task for this operation.</returns>
        public Task ExecuteHandlerFilterAsync(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func<Task> continuation)
        {
            if (handlerContext == null)
            {
                throw Error.ArgumentNull("handlerContext");
            }

            if (continuation == null)
            {
                throw Error.ArgumentNull("continuation");
            }
            
            return this.ExecuteHandlerFilterAsyncCore(handlerContext, cancellationToken, continuation);
        }
コード例 #17
0
 /// <summary>
 /// Occurs before the handle method is invoked.
 /// </summary>
 /// <param name="eventOccurringContext">The handler context.</param>
 /// <remarks>
 /// Overrides this method to add a behaviour before an event in a non-asynchronous way.
 /// </remarks>
 public virtual void OnEventOccurring(EventHandlerContext eventOccurringContext)
 {
 }
コード例 #18
0
 private static Func<Task> InvokeActionWithActionFilters(EventHandlerContext context, CancellationToken cancellationToken, IEventHandlerFilter[] filters, Func<ActionInvoker, Task> innerAction, ActionInvoker state)
 {
     return InvokeHandlerWithHandlerFiltersAsync(context, cancellationToken, filters, () => innerAction(state));
 }
コード例 #19
0
        private async Task ExecuteHandlerFilterAsyncCore(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func<Task> continuation)
        {
            await this.OnEventOccurringAsync(handlerContext, cancellationToken);

            await this.CallOnHandlerOccurrededAsync(handlerContext, cancellationToken, continuation);
        }
コード例 #20
0
        private async Task ExecuteHandlerFilterAsyncCore(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func <Task> continuation)
        {
            await this.OnEventOccurringAsync(handlerContext, cancellationToken);

            await this.CallOnHandlerOccurrededAsync(handlerContext, cancellationToken, continuation);
        }
コード例 #21
0
        private async Task CallOnHandlerOccurrededAsync(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func<Task> continuation)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ExceptionDispatchInfo exceptionInfo = null;
            try
            {
                await continuation();
            }
            catch (Exception e)
            {
                exceptionInfo = ExceptionDispatchInfo.Capture(e);
            }

            EventHandlerOccurredContext occurredContext = new EventHandlerOccurredContext(handlerContext, exceptionInfo);

            await this.OnEventOccurredAsync(occurredContext, cancellationToken);

            if (occurredContext.ExceptionInfo != null)
            {
                if (exceptionInfo == null)
                {
                    occurredContext.ExceptionInfo.Throw();
                }
                else
                {
                    Exception exception = exceptionInfo.SourceException;
                    Exception newException = occurredContext.ExceptionInfo.SourceException;
                    if (newException == exception)
                    {
                        exceptionInfo.Throw();
                    }
                    else
                    {
                        occurredContext.ExceptionInfo.Throw();
                    }
                }
            }
        }
コード例 #22
0
 /// <summary>
 /// Occurs before the handle method is invoked.
 /// </summary>
 /// <param name="eventOccurringContext">The handler context.</param>
 /// <remarks>
 /// Overrides this method to add a behaviour before an event in a non-asynchronous way.
 /// </remarks>
 public virtual void OnEventOccurring(EventHandlerContext eventOccurringContext)
 {
 }
コード例 #23
0
            public Task Execute(EventHandlerContext context)
            {
                Contract.Requires(context != null);

                return(this.executor(context.Handler, context.Event));
            }