public virtual Task OnEventOccurredAsync(EventHandlerOccurredContext eventOccurredContext, CancellationToken cancellationToken) { try { this.OnEventOccurred(eventOccurredContext); } catch (Exception ex) { return TaskHelpers.FromError(ex); } return TaskHelpers.Completed(); }
/// <summary> /// Occurs after the handle method is invoked. /// </summary> /// <param name="eventOccurredContext">The handler executed context.</param> /// <remarks> /// Overrides this method to add a behaviour after an event in a non-asynchronous way. /// </remarks> public virtual void OnEventOccurred(EventHandlerOccurredContext eventOccurredContext) { }
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(); } } } }