Exemplo n.º 1
0
        // Dispatches the query to the handling consumer.
        public async Task <object> Dispatch(IQuery query, IQueryConsumer consumer, CancellationToken cancellationToken)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (consumer == null)
            {
                throw new ArgumentNullException(nameof(consumer));
            }

            var taskSource = new TaskCompletionSource <object>();

            try
            {
                if (IsAsync)
                {
                    var invokeParams = new List <object> {
                        consumer, query
                    };
                    if (IsCancellable)
                    {
                        invokeParams.Add(cancellationToken);
                    }

                    var asyncResult = (Task)Invoker.DynamicInvoke(invokeParams.ToArray());
                    await asyncResult.ConfigureAwait(false);

                    object result = ProcessResult(query, asyncResult);
                    taskSource.SetResult(result);
                }
                else
                {
                    object syncResult = Invoker.DynamicInvoke(consumer, query);
                    object result     = ProcessResult(query, syncResult);
                    taskSource.SetResult(result);
                }
            }
            catch (Exception ex)
            {
                var invokeEx = ex as TargetInvocationException;
                var sourceEx = ex;

                if (invokeEx != null)
                {
                    sourceEx = invokeEx.InnerException;
                }

                var dispatchEx = new QueryDispatchException("Exception Dispatching Query to Consumer.",
                                                            this,
                                                            sourceEx);

                taskSource.SetException(dispatchEx);
            }

            return(await taskSource.Task.ConfigureAwait(false));
        }
Exemplo n.º 2
0
 private void LogQueryDispatch(IQuery query, IQueryConsumer consumer)
 {
     _logger.LogTraceDetails($"Dispatching Query Type: {query.GetType()} to Consumer: {consumer.GetType()}", query);
 }