public async Task <string> HandleRequestAsync(RpcPath requestPath, string requestBody, IRouteContext routeContext)
        {
            try
            {
                List <RpcRequest> requests = this.parser.ParseRequests(requestBody, out bool isBulkRequest, this.serverConfig.Value.JsonSerializerSettings);
                this.logger?.LogInformation($"Processing {requests.Count} Rpc requests");

                int batchLimit = this.serverConfig.Value.BatchRequestLimit;
                List <RpcResponse> responses;
                if (batchLimit > 0 && requests.Count > batchLimit)
                {
                    string batchLimitError = $"Request count exceeded batch request limit ({batchLimit}).";
                    responses = new List <RpcResponse>
                    {
                        new RpcResponse(null, new RpcError(RpcErrorCode.InvalidRequest, batchLimitError))
                    };
                    this.logger?.LogError(batchLimitError + " Returning error response.");
                }
                else
                {
                    responses = await this.invoker.InvokeBatchRequestAsync(requests, requestPath, routeContext);
                }
                if (responses == null || !responses.Any())
                {
                    this.logger?.LogInformation("No rpc responses created.");
                    return(null);
                }

                string resultJson = !isBulkRequest
                                        ? JsonConvert.SerializeObject(responses.Single(), this.serverConfig.Value.JsonSerializerSettings)
                                        : JsonConvert.SerializeObject(responses, this.serverConfig.Value.JsonSerializerSettings);


                this.logger?.LogInformation($"{responses.Count} rpc response(s) created.");
                return(resultJson);
            }
            catch (RpcException ex)
            {
                this.logger?.LogException(ex, "Error occurred when proccessing Rpc request. Sending Rpc error response");
                var response = new RpcResponse(null, new RpcError(ex, this.serverConfig.Value.ShowServerExceptions));
                return(JsonConvert.SerializeObject(response, this.serverConfig.Value.JsonSerializerSettings));
            }
        }
        public async Task <string> HandleRequestAsync(RpcPath requestPath, string requestBody, IRouteContext routeContext)
        {
            try
            {
                ParsingResult result = this.parser.ParseRequests(requestBody);
                this.logger?.LogInformation($"Processing {result.RequestCount} Rpc requests");

                int?batchLimit = this.serverConfig.Value.BatchRequestLimit;
                List <RpcResponse> responses = new List <RpcResponse>();
                if (batchLimit > 0 && result.RequestCount > batchLimit)
                {
                    string batchLimitError = $"Request count exceeded batch request limit ({batchLimit}).";
                    responses = new List <RpcResponse>
                    {
                        new RpcResponse(null, new RpcError(RpcErrorCode.InvalidRequest, batchLimitError))
                    };
                    this.logger?.LogError(batchLimitError + " Returning error response.");
                }
                else
                {
                    if (result.Requests.Any())
                    {
                        responses = await this.invoker.InvokeBatchRequestAsync(result.Requests, requestPath, routeContext);
                    }
                    else
                    {
                        responses = new List <RpcResponse>();
                    }
                    foreach ((RpcId id, RpcError error) in result.Errors)
                    {
                        if (id == default)
                        {
                            this.logger.LogError($"Request with no id failed and no response will be sent. Error - Code: {error.Code}, Message: {error.Message}");
                            continue;
                        }
                        responses.Add(new RpcResponse(id, error));
                    }
                }
                if (responses == null || !responses.Any())
                {
                    this.logger?.LogInformation("No rpc responses created.");
                    return(null);
                }
                this.logger?.LogInformation($"{responses.Count} rpc response(s) created.");

                if (result.IsBulkRequest)
                {
                    return(this.responseSerializer.SerializeBulk(responses));
                }
                else
                {
                    return(this.responseSerializer.Serialize(responses.Single()));
                }
            }
            catch (RpcException ex)
            {
                this.logger?.LogException(ex, "Error occurred when proccessing Rpc request. Sending Rpc error response");
                var response = new RpcResponse(null, ex.ToRpcError(this.serverConfig.Value.ShowServerExceptions));
                return(this.responseSerializer.Serialize(response));
            }
        }
Beispiel #3
0
        public async Task <bool> HandleRequestAsync(Stream requestBody, Stream responseBody)
        {
            try
            {
                ParsingResult result = this.parser.ParseRequests(requestBody);
                this.logger.ProcessingRequests(result.RequestCount);

                int?batchLimit = this.serverConfig.Value.BatchRequestLimit;
                List <RpcResponse> responses = new List <RpcResponse>();
                if (batchLimit > 0 && result.RequestCount > batchLimit)
                {
                    string batchLimitError = $"Request count exceeded batch request limit ({batchLimit}).";
                    responses = new List <RpcResponse>
                    {
                        new RpcResponse(new RpcId(), new RpcError(RpcErrorCode.InvalidRequest, batchLimitError))
                    };
                    this.logger.LogError(batchLimitError + " Returning error response.");
                }
                else
                {
                    if (result.Requests.Any())
                    {
                        responses = await this.invoker.InvokeBatchRequestAsync(result.Requests);
                    }
                    else
                    {
                        responses = new List <RpcResponse>();
                    }
                    foreach ((RpcId id, RpcError error) in result.Errors)
                    {
                        if (id == default)
                        {
                            this.logger.ResponseFailedWithNoId(error.Code, error.Message);
                            continue;
                        }
                        responses.Add(new RpcResponse(id, error));
                    }
                }
                if (responses == null || !responses.Any())
                {
                    this.logger.NoResponses();
                    return(false);
                }
                this.logger.Responses(responses.Count);

                if (result.IsBulkRequest)
                {
                    await this.responseSerializer.SerializeBulkAsync(responses, responseBody);
                }
                else
                {
                    await this.responseSerializer.SerializeAsync(responses.Single(), responseBody);
                }
            }
            catch (RpcException ex)
            {
                this.logger.LogException(ex, "Error occurred when proccessing Rpc request. Sending Rpc error response");
                var response = new RpcResponse(new RpcId(), ex.ToRpcError(this.serverConfig.Value.ShowServerExceptions));
                await this.responseSerializer.SerializeAsync(response, responseBody);
            }
            return(true);
        }