示例#1
0
        /// <summary>
        /// Execute the request via the worker.
        /// </summary>
        /// <param name="request">The <see cref="HandlerRequest"/> to execute.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to cancel the execution.</param>
        /// <returns>The result of the command, if any.</returns>
        public Task <HandlerResponse> ExecuteAsync(CommandHandlerRequest request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            cancellationToken.ThrowIfCancellationRequested();
            ServicesContainer        servicesContainer = request.Configuration.Services;
            ICommandHandlerSelector  handlerSelector   = servicesContainer.GetHandlerSelector();
            CommandHandlerDescriptor descriptor        = handlerSelector.SelectHandler(request);

            ICommandHandler commandHandler = descriptor.CreateHandler(request);

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

            request.RegisterForDispose(commandHandler, true);
            CommandHandlerContext context = new CommandHandlerContext(request, descriptor);

            context.Handler = commandHandler;
            commandHandler.CommandContext = context;

            CommandFilterGrouping commandFilterGrouping = descriptor.GetFilterGrouping();

            ICommandHandlerResult result = new CommandHandlerFilterResult(context, servicesContainer, commandFilterGrouping.CommandHandlerFilters);

            if (descriptor.RetryPolicy != RetryPolicy.NoRetry)
            {
                result = new RetryHandlerResult(descriptor.RetryPolicy, result);
            }

            if (commandFilterGrouping.ExceptionFilters.Length > 0)
            {
                IExceptionLogger  exceptionLogger  = ExceptionServices.GetLogger(servicesContainer);
                IExceptionHandler exceptionHandler = ExceptionServices.GetHandler(servicesContainer);
                result = new ExceptionFilterResult(context, commandFilterGrouping.ExceptionFilters, exceptionLogger, exceptionHandler, result);
            }

            return(result.ExecuteAsync(cancellationToken));
        }