public void Handle(ProcessingCommand processingCommand) { var command = processingCommand.Message; if (string.IsNullOrEmpty(command.AggregateRootId)) { var errorMessage = string.Format("The aggregateRootId of command cannot be null or empty. commandType:{0}, commandId:{1}", command.GetType().Name, command.Id); _logger.Error(errorMessage); CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage); return; } ICommandHandlerProxy commandHandler; var findResult = GetCommandHandler(processingCommand, commandType => _commandHandlerProvider.GetHandlers(commandType), out commandHandler); if (findResult == HandlerFindResult.Found) { HandleCommand(processingCommand, commandHandler); } else if (findResult == HandlerFindResult.TooManyHandlerData) { _logger.ErrorFormat("Found more than one command handler data, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id); CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command handler data found."); } else if (findResult == HandlerFindResult.TooManyHandler) { _logger.ErrorFormat("Found more than one command handler, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id); CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command handler found."); } else if (findResult == HandlerFindResult.NotFound) { ICommandAsyncHandlerProxy commandAsyncHandler; findResult = GetCommandHandler(processingCommand, commandType => _commandAsyncHandlerProvider.GetHandlers(commandType), out commandAsyncHandler); if (findResult == HandlerFindResult.Found) { HandleCommand(processingCommand, commandAsyncHandler); } else if (findResult == HandlerFindResult.TooManyHandlerData) { _logger.ErrorFormat("Found more than one command async handler data, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id); CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command async handler data found."); } else if (findResult == HandlerFindResult.TooManyHandler) { _logger.ErrorFormat("Found more than one command async handler, commandType:{0}, commandId:{1}", command.GetType().FullName, command.Id); CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, "More than one command async handler found."); } else if (findResult == HandlerFindResult.NotFound) { var errorMessage = string.Format("No command handler found of command. commandType:{0}, commandId:{1}", command.GetType().Name, command.Id); _logger.Error(errorMessage); CompleteCommand(processingCommand, CommandStatus.Failed, typeof(string).FullName, errorMessage); } } }
private ICommandAsyncHandlerProxy GetCommandAsyncHandler(ProcessingCommand processingCommand) { var command = processingCommand.Message; var commandAsyncHandlers = _commandAsyncHandlerProvider.GetHandlers(command.GetType()); if (commandAsyncHandlers.Count() > 1) { _logger.ErrorFormat("Found more than one command handlers, commandType:{0}, commandId:{1}.", command.GetType().FullName, command.Id); NotifyCommandExecuted(processingCommand, CommandStatus.Failed, null, "More than one command handlers found."); return(null); } return(commandAsyncHandlers.SingleOrDefault()); }