コード例 #1
0
 internal static CommandHandlerDelegate FromCommandHandler <TCommand>(ICommandAsyncHandler <TCommand> commandAsyncHandler)
     where TCommand : class, ICommand
 {
     return(new CommandHandlerDelegate(async(inputCommand, ct) =>
     {
         TCommand command = EnsureValidCommand <TCommand>(inputCommand);
         await commandAsyncHandler.HandleAsync(command, ct).ConfigureAwait(false);
     }));
 }
コード例 #2
0
        internal static MessageHandlerDelegate FromCommandHandler <TCommand>(ICommandAsyncHandler <TCommand> commandAsyncHandler)
            where TCommand : class
        {
            if (commandAsyncHandler == null)
            {
                throw new ArgumentNullException(nameof(commandAsyncHandler));
            }

            return((inputCommand, cancellationToken) =>
                   commandAsyncHandler.HandleAsync((TCommand)inputCommand ?? throw new ArgumentException("Invalid command.", nameof(inputCommand)), cancellationToken));
        }
コード例 #3
0
        internal static CommandHandlerDelegate FromFactory <TCommand>(Func <ICommandAsyncHandler <TCommand> > commandHandlerFactory)
            where TCommand : class, ICommand
        {
            return(new CommandHandlerDelegate(async(inputCommand, ct) =>
            {
                TCommand command = EnsureValidCommand <TCommand>(inputCommand);
                ICommandAsyncHandler <TCommand> instance = EnsureInstanceFromFactory(commandHandlerFactory);

                await instance.HandleAsync(command, ct).ConfigureAwait(false);
            }));
        }
コード例 #4
0
        /// <summary>
        /// <para>Resolves an instance of ICommandAsyncHandler<TCommand> from the container</para>
        /// <para>and converts it to a command handler delegate which can be invoked to process the command.</para>
        /// </summary>
        /// <typeparamref name="TCommand">Type of command which is handled by the command handler.</typeparamref>
        /// <returns>Instance of <see cref="CommandHandlerDelegate"/> which executes the command handler processing.</returns>
        public CommandHandlerDelegate ResolveCommandHandler <TCommand>() where TCommand : class, ICommand
        {
            try
            {
                // Try to resolve async handler first.
                ICommandAsyncHandler <TCommand> commandAsyncHandler = _containerAdapter.Resolve <ICommandAsyncHandler <TCommand> >();

                if (commandAsyncHandler == null)
                {
                    // No handlers are resolved. Throw exception.
                    throw ExceptionBuilder.NoCommandHandlerResolvedException(typeof(TCommand));
                }

                return(CommandHandlerDelegateBuilder.FromCommandHandler(commandAsyncHandler));
            }
            catch (Exception ex)
            {
                // No handlers are resolved. Throw exception.
                throw ExceptionBuilder.NoCommandHandlerResolvedException(typeof(TCommand), ex);
            }
        }
コード例 #5
0
        async Task AsyncHandleCommandAsync(ProcessingCommand processingCommand, ICommandAsyncHandler asyncHandler)
        {
            var command          = processingCommand.Command;
            var commandType      = command.GetType();
            var asyncHandlerType = asyncHandler.GetType();

            try
            {
                var asyncHandlerMethod = asyncHandlerType.GetTypeInfo().GetMethod("HandleAsync", new[] { commandType });
                var result             = await(Task <AsyncExecutedResult <IApplicationMessage> >) asyncHandlerMethod.Invoke(asyncHandler, new [] { command });

                if (result.Succeeded)
                {
                    var applicationMessage = result.Data;

                    if (applicationMessage != null)
                    {
                        logger.LogDebug($"异步命令处理器执行成功,发布产生的应用消息。 [CommandType = {commandType}, CommandId = {command.Id}, CommandAsyncHandlerType = {asyncHandlerType}, ApplicationMessageType = {applicationMessage.GetType()}, ApplicationMessageId = {applicationMessage.Id}, ApplicationMessageRoutingKey = {applicationMessage.GetRoutingKey()}]");
                        await PublishApplicationMessageAsync(processingCommand, applicationMessage);
                    }
                    else
                    {
                        logger.LogDebug($"异步命令处理器执行成功,未产生任何应用消息。 [CommandType = {commandType}, CommandId = {command.Id}, CommandAsyncHandlerType = {asyncHandlerType}]");
                        await processingCommand.OnQueueProcessedAsync(CommandExecutedStatus.Succeeded, "命令异步处理器执行成功,未产生任何应用消息。");
                    }
                }
                else
                {
                    logger.LogError($"异步命令处理器执行失败。 [CommandType = {commandType}, CommandId = {command.Id}, CommandAsyncHandlerType = {asyncHandlerType}]");
                    await processingCommand.OnQueueProcessedAsync(CommandExecutedStatus.Failed, "异步命令处理器执行失败。");
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"异步命令处理器执行失败。 [CommandType = {commandType}, CommandId = {command.Id}, CommandAsyncHandlerType = {asyncHandlerType}]");
                await processingCommand.OnQueueProcessedAsync(CommandExecutedStatus.Failed, "异步命令处理器执行失败。");
            }
        }
        // Note: When renaming or changing signature of this ResolveMessageHandler method,
        // the cached ResolveMessageHandlerOpenGenericMethodInfo should be updated.

        /// <summary>
        /// Resolves an instance of <see cref="Xer.Cqrs.CommandStack.ICommandAsyncHandler{TCommand}"/> from the container
        /// and converts it to a message handler delegate which processes the command when invoked.
        /// </summary>
        /// <typeparamref name="commandType">Type of command which is handled by the command handler.</typeparamref>
        /// <returns>Instance of <see cref="Xer.Delegator.MessageHandlerDelegate"/> which executes the command handler processing when invoked.</returns>
        public MessageHandlerDelegate ResolveMessageHandler <TCommand>() where TCommand : class
        {
            try
            {
                // Try to resolve async handler first.
                ICommandAsyncHandler <TCommand> commandAsyncHandler = _containerAdapter.Resolve <ICommandAsyncHandler <TCommand> >();
                if (commandAsyncHandler != null)
                {
                    return(CommandHandlerDelegateBuilder.FromCommandHandler(commandAsyncHandler));
                }
            }
            catch (Exception ex)
            {
                bool exceptionHandled = _exceptionHandler?.Invoke(ex) ?? false;
                if (!exceptionHandled)
                {
                    // Exception while resolving handler. Throw exception.
                    throw new NoMessageHandlerResolvedException($"Error encoutered while trying to retrieve an instance of {typeof(ICommandAsyncHandler<TCommand>)} from the container.", typeof(TCommand), ex);
                }
            }

            return(NullMessageHandlerDelegate.Instance);
        }
コード例 #7
0
 public CommandAsyncHandlerProxy(ICommandAsyncHandler <TCommand> commandHandler, Type commandHandlerType)
 {
     _commandHandler     = commandHandler;
     _commandHandlerType = commandHandlerType;
 }
コード例 #8
0
 public CommandAsyncHandlerProxy(ICommandAsyncHandler <TCommand> commandHandler)
 {
     _commandHandler = commandHandler;
 }