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); }
public ActionInvoker(EventHandlerContext context, CancellationToken cancellationToken, ServicesContainer services) { Contract.Requires(services != null); this.context = context; this.cancellationToken = cancellationToken; this.services = services; }
/// <inheritdocs /> public virtual Task InvokeHandlerAsync(EventHandlerContext context, CancellationToken cancellationToken) { if (context == null) { throw Error.ArgumentNull("context"); } return(InvokeActionAsyncCore(context, cancellationToken)); }
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)); }
/// <inheritdocs /> public virtual Task InvokeHandlerAsync(EventHandlerContext context, CancellationToken cancellationToken) { if (context == null) { throw Error.ArgumentNull("context"); } return InvokeActionAsyncCore(context, cancellationToken); }
/// <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; }
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; }
public virtual Task OnEventOccurringAsync(EventHandlerContext eventOccurringContext, CancellationToken cancellationToken) { try { this.OnEventOccurring(eventOccurringContext); } catch (Exception ex) { return TaskHelpers.FromError(ex); } return TaskHelpers.Completed(); }
public virtual Task OnEventOccurringAsync(EventHandlerContext eventOccurringContext, CancellationToken cancellationToken) { try { this.OnEventOccurring(eventOccurringContext); } catch (Exception ex) { return(TaskHelpers.FromError(ex)); } return(TaskHelpers.Completed()); }
/// <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)); }
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; }
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)); } }
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(); } } } }
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); }
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)); }
/// <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); }
/// <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) { }
private static Func<Task> InvokeActionWithActionFilters(EventHandlerContext context, CancellationToken cancellationToken, IEventHandlerFilter[] filters, Func<ActionInvoker, Task> innerAction, ActionInvoker state) { return InvokeHandlerWithHandlerFiltersAsync(context, cancellationToken, filters, () => innerAction(state)); }
private async Task ExecuteHandlerFilterAsyncCore(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func<Task> continuation) { await this.OnEventOccurringAsync(handlerContext, cancellationToken); await this.CallOnHandlerOccurrededAsync(handlerContext, cancellationToken, continuation); }
private async Task ExecuteHandlerFilterAsyncCore(EventHandlerContext handlerContext, CancellationToken cancellationToken, Func <Task> continuation) { await this.OnEventOccurringAsync(handlerContext, cancellationToken); await this.CallOnHandlerOccurrededAsync(handlerContext, cancellationToken, continuation); }
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(); } } } }
public Task Execute(EventHandlerContext context) { Contract.Requires(context != null); return(this.executor(context.Handler, context.Event)); }