public async Task DispatchRedRequest( HttpContext context, RequestDelegate next, RequestMessage message, CancellationToken cancellationToken ) { var contentType = message.Headers.ContentType; var responseMessage = new ResponseMessage(); IMessageReaderWriter readerWriter; if (contentType != null && contentType.StartsWith("multipart/")) { readerWriter = MultipartMessageReaderWriter; responseMessage.Headers.ContentType = "multipart/mixed"; } else //if (contentType == "application/json" || contentType == "text/json") { readerWriter = JsonMessageReaderWriter; responseMessage.Headers.ContentType = contentType == "text/json" ? contentType : "application/json"; } var response = context.Response; var responseStream = context.Response.Body; if (message is RequestAggregationMessage aggregationMessage) { var requestToResponseAdapter = new AsyncEnumerableAdapter <RequestMessage, ResponseMessage>( aggregationMessage.UnpackAsync(cancellationToken), request => ProgressPipeline(context, next, request, responseMessage, cancellationToken) ); var responseContentType = readerWriter.GetResponseContentTypeFromRequestContentType(contentType); var headers = response.Headers; headers.Add("Content-Type", responseContentType); await readerWriter.WriteResponseMessageToStream(responseStream, requestToResponseAdapter, cancellationToken); } else { var pipelineResponse = await ProgressPipeline(context, next, message, responseMessage, cancellationToken); await readerWriter.WriteResponseMessageToStream(responseStream, pipelineResponse, cancellationToken); await MessageConverter.CopyResponseMessageToTarget(response, pipelineResponse, cancellationToken); } }