internal static Task Execute(this MessageInvoker invoker, DispatchingContext context) { var invocationResult = invoker(context.HandlerInstance, context.MessageInstance); var awaitableResult = invocationResult as Task; if (awaitableResult == null) { context.Result = invocationResult; return(Task.CompletedTask); } if (awaitableResult.Status == TaskStatus.Created) { awaitableResult.Start(); } return(awaitableResult.ContinueWith(task => { if (task.IsFaulted) { //TODO throw appropriate exception! //throw taskResult.Exception; } if (task.IsCanceled) { throw new OperationCanceledException("The async Task operation was cancelled"); } if (!task.IsCompleted) { throw new InvalidOperationException("Unknown Task state"); } context.Result = task.GetResult(); })); }
private async Task <object> DispatchMessageAsync(object message, Subscription subscription) { object handler = ServiceProvider.GetMessageHandler(subscription.HandlerType); List <IContextInterceptor> interceptors = ServiceProvider.GetInterceptors(_interceptorsTypes); var context = new DispatchingContext(handler, message); interceptors.ForEach(async interceptor => await interceptor.OnExecuting(context)); try { await subscription.Invoker.Execute(context); } catch (Exception exception) { interceptors.ForEach(async interceptor => await interceptor.OnException(context, exception)); throw; } interceptors.ForEach(async interceptor => await interceptor.OnExecuted(context)); return(context.Result); }