Esempio n. 1
0
        public async Task <IActionResult> CalculateSum([FromBody] CalculateSumRequest request)
        {
            var tasks = request.Requests.AsParallel().Select(async r =>
            {
                var result = await SecondaryPipeline.Invoke(r.Uri, requestServices: HttpContext.RequestServices);
                if (result.StatusCode != 200)
                {
                    return((double?)null);
                }

                var match = NumberExpression.Match(result.ResponseBody);
                if (!match.Success)
                {
                    return((double?)null);
                }

                return(double.Parse(match.ToString()) * r.Coefficient);
            });

            var taskResults = await Task.WhenAll(tasks);

            if (taskResults.Any(d => !d.HasValue))
            {
                return(BadRequest());
            }

            return(Ok($"[{taskResults.Sum()}]"));
        }
        public async Task <IActionResult> RunBatch([FromBody] RunBatchRequest request)
        {
            var tasks = request.Requests.AsParallel().Select(async(r, i) =>
            {
                var result = await SecondaryPipeline.Invoke(r.Uri, requestServices: HttpContext.RequestServices);
                return(new RunBatchResponseItem
                {
                    Index = i,
                    StatusCode = result.StatusCode,
                    Body = result.ResponseBody
                });
            });

            var taskResults = await Task.WhenAll(tasks);

            return(Ok(new RunBatchResponse {
                Responses = taskResults.OrderBy(r => r.Index).ToList()
            }));
        }