Ejemplo n.º 1
0
        public async Task <ExecuteCommandResultDto> ExecuteAsync(ExecuteCommandDto dto)
        {
            var command = ExecuteCommandDto.Deserialize(dto);

            LoggerThreadContextManager.MarkCommandExecution(command);
            Logger.InfoFormat("Ask execution of command {0} - {1}", command.MessageId, command.Describe());

            var user = command.GetContextData(MessagesConstants.UserId) ?? dto.ImpersonatingUser;

            if (user == null)
            {
                throw new JarvisFrameworkEngineException($"Unable to execue a command, no user in header {MessagesConstants.UserId} nor impersonation user in dto is present.");
            }

            try
            {
                await _commandBus.SendAsync(command, user).ConfigureAwait(false);

                return(new ExecuteCommandResultDto(true, "", null));
            }
            catch (AggregateModifiedException ex)
            {
                //we have a conflicting exception
                var retValue = new ExecuteCommandResultDto(false, ex.Message, ex);
                if (dto.OfflineCheckpointTokenFrom.HasValue)
                {
                    List <CommitShortInfo> newCommits = await GetNewCommid(dto.OfflineCheckpointTokenFrom.Value, ex.AggregateId).ConfigureAwait(false);

                    var idList = newCommits.Select(c => c.OperationId).ToList();
                    retValue.ConflictingCommands = GetConflictingCommandList(ex.AggregateId, idList);
                }

                return(retValue);
            }
            catch (Exception ex)
            {
                return(new ExecuteCommandResultDto(false, ex.Message, ex));
            }
            finally
            {
                LoggerThreadContextManager.ClearMarkCommandExecution(); //clear data
            }
        }
        public static ICommand Deserialize(ExecuteCommandDto dto)
        {
            var type = System.Type.GetType(dto.Type);

            if (type == null)
            {
                throw new JarvisFrameworkEngineException($"Unable to load command type {dto.Type}");
            }

            var jsonSerializerSettings = GetSerializationSettings();
            var rawDeserializedObject  = JsonConvert.DeserializeObject(dto.SerializedCommand, type, jsonSerializerSettings);

            var command = rawDeserializedObject as ICommand;

            if (command == null)
            {
                throw new JarvisFrameworkEngineException($"Deserialzied type {dto.Type} does not implements ICommand interface");
            }

            return(command);
        }