Пример #1
0
        private async Task <CommandResult> ExecuteCommand(HttpContext httpContext, string commandName, Guid commandId)
        {
            if (_registry.TryGetValue(commandName, out var commandTypeInformation))
            {
                var(commandType, commandTypeHandler) = commandTypeInformation;
                var cancellationToken = httpContext.RequestAborted;
                var command           = await httpContext.Request.BodyReader.DeserializeBodyAsync(
                    commandType,
                    _jsonSerializerOptions,
                    cancellationToken);

                try
                {
                    return(await commandTypeHandler.HandleCommand(command, commandId,
                                                                  httpContext.RequestServices, cancellationToken));
                }
                catch (TargetInvocationException e) when(e.InnerException is CommandExecutionException)
                {
                    // _logger.LogError(e.InnerException.Message);
                    return(CommandResult.Failure(e.InnerException.Message));
                }
                catch (TargetInvocationException e) when(e.InnerException is AggregateException aggregateException)
                {
                    return(HandleAggregateException(aggregateException));
                }
                catch (AggregateException aggregateException)
                {
                    return(HandleAggregateException(aggregateException));
                }
            }

            throw new NullReferenceException($"Command or command handler for {commandName} was not found");
        }