'Stream was already consumed' 在 ASP.NET Core 中使用 Polly 重试请求时出错

'Stream was already consumed' error using Polly to retry requests in ASP.NET Core

我有一个 ASP.NET Core 3.1 Web API 服务正在接收一个 Web 请求,对其进行一些操作,然后将其传递给后端服务并同步返回响应。它工作正常,但我想为这些后端请求引入一些重试逻辑,以防出现一些问题。

我正在使用类型化的 HttpClient 并尝试使用 Polly 来实现重试逻辑: https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory#using-polly-with-ihttpclientfactory

当后端服务工作时,一切似乎都很好,但不幸的是,每当我的后端 returns 出现类似 500 Internal Server Error 的错误时,我都会收到以下异常:

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware: Error: An unhandled exception has occurred while executing the request.

System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.InvalidOperationException: The stream was already consumed. It cannot be read again.
   at System.Net.Http.StreamContent.PrepareContent()
   at System.Net.Http.StreamContent.SerializeToStreamAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
   at System.Net.Http.HttpContent.CopyToAsync(Stream stream, TransportContext context, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Polly.Retry.AsyncRetryEngine.ImplementationAsync[TResult](Func`3 action, Context context, CancellationToken cancellationToken, ExceptionPredicates shouldRetryExceptionPredicates, ResultPredicates`1 shouldRetryResultPredicates, Func`5 onRetryAsync, Int32 permittedRetryCount, IEnumerable`1 sleepDurationsEnumerable, Func`4 sleepDurationProvider, Boolean continueOnCapturedContext)
   at Polly.AsyncPolicy`1.ExecuteAsync(Func`3 action, Context context, CancellationToken cancellationToken, Boolean continueOnCapturedContext)
   at Microsoft.Extensions.Http.PolicyHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at MyProject.MyController.Routing.HttpMessageRouter.SendRequestAndTrackTiming(Func`1 action, String destinationID) in /mnt/c/Users/myCode/source/repos/MyProject-GitLab/MyController/src/MyController/Routing/HttpMessageRouter.cs:line 59
   at MyProject.MyController.Routing.HttpMessageRouter.SendNewRequest(IMessageWrapper`1 message) in /mnt/c/Users/myCode/source/repos/MyProject-GitLab/MyController/src/MyController/Routing/HttpMessageRouter.cs:line 33
   at MyProject.MyController.Controllers.MyControllerController.Resource(String destinationId) in /mnt/c/Users/myCode/source/repos/MyProject-GitLab/MyController/src/MyController/Controllers/MyControllerController.cs:line 151
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Prometheus.HttpMetrics.HttpRequestDurationMiddleware.Invoke(HttpContext context)
   at Prometheus.HttpMetrics.HttpRequestCountMiddleware.Invoke(HttpContext context)
   at Prometheus.HttpMetrics.HttpInProgressMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

我必须发送请求的代码如下。第一种方法实际上只使用委托,因此我可以透明地添加一些围绕类型化 HttpClient 调用收集的指标。类型化的 HttpClient 在这段代码中被称为 RoutingClient:

public async Task<IActionResult> SendNewRequest(IMessageWrapper<HttpRequestMessage> message)
{
    HttpResponseMessage destinationResponse = await SendRequestAndTrackTiming(() => _client.SendAsync(message.Message), message.DestinationID);
    return CreateSerializeableResponseMessage(destinationResponse);
}

private ResponseMessageResult CreateSerializeableResponseMessage(HttpResponseMessage httpResponse)
{
    ResponseMessageResult responseMessage = new ResponseMessageResult(httpResponse);
    IOutputFormatter[] formattersList = { new HttpResponseMessageOutputFormatter() };
    FormatterCollection<IOutputFormatter> formattersCollection = new FormatterCollection<IOutputFormatter>(formattersList);
    responseMessage.Formatters = formattersCollection;
    return responseMessage;
}

private async Task<HttpResponseMessage> SendRequestAndTrackTiming(Func<Task<HttpResponseMessage>> action, string destinationID)
{
    HttpResponseMessage response = null;

    Stopwatch stopwatch = new Stopwatch();
    try
    {
        stopwatch.Start();
        response = await action();
        return response;
    }
    finally
    {
        stopwatch.Stop();
        HttpStatusCode statusCode = (response != null) ? response.StatusCode : HttpStatusCode.InternalServerError;
        _routedMessageMetricTracker.Histogram.Observe(destinationID, statusCode, stopwatch.Elapsed.TotalSeconds);
    }
}

这是我在 Startup.ConfigureServices() 中的代码(实际上这是在我定义的扩展方法中):

public static IServiceCollection AddRoutingClient(this IServiceCollection services, RoutingSettings routingSettings)
{
    List<TimeSpan> retryTimeSpans = new List<TimeSpan>();

    // routingSettings.RetrySeconds is just an array of double values.
    foreach (double retrySeconds in routingSettings.RetrySeconds)
    {
        if (retrySeconds >= 0) retryTimeSpans.Add(TimeSpan.FromSeconds(retrySeconds));
    }

    services.AddHttpClient<IRoutingClient, RoutingClient>()
        .AddPolicyHandler((services, request) => HttpPolicyExtensions.HandleTransientHttpError()
            .WaitAndRetryAsync(retryTimeSpans, onRetry: (outcome, timespan, retryAttempt, context) =>
                {
                    services.GetService<ILogger<RoutingClient>>()?
                        .LogWarning($"Delaying for {timespan.TotalMilliseconds}ms, then making retry {retryAttempt}.");
                }
            ));

    return services;
}

我真的很想使用 Polly 方法,因为它看起来很干净,但我不确定我在这里做错了什么。我一定是做错了什么,因为我希望这是应该处理的 Polly 的一个非常常见的用例。

原来我的问题不是由上面处理响应的代码引起的。相反,它实际上是由我的代码引起的,该代码正在操纵 request.

我正在通过 HttpContext.GetHttpRequestMessage() 方法将传入请求作为 HttpRequestMessage 对象读取,并且我试图通过调用我输入的内容来重新使用同一个对象以传递到后端服务HttpClient。但是,该请求的内容流是只读一次的,因此我必须按照对另一个 post 的回答中所述制作该 HttpRequestMessage 的副本:

这是该答案代码的稍微详细的版本:

private static async Task<HttpRequestMessage> CloneHttpRequestMessageAsync(HttpRequestMessage request)
{
    HttpRequestMessage copyOfRequest = new HttpRequestMessage(request.Method, request.RequestUri);

    // Copy the request's content (via a MemoryStream) into the cloned object
    var ms = new MemoryStream();
    if (request.Content != null)
    {
        await request.Content.CopyToAsync(ms).ConfigureAwait(false);
        ms.Position = 0;
        copyOfRequest.Content = new StreamContent(ms);

        // Copy the content headers
        if (request.Content.Headers != null)
        {
            foreach (var h in request.Content.Headers)
            {
                copyOfRequest.Content.Headers.Add(h.Key, h.Value);
            }
        }
    }

    copyOfRequest.Version = request.Version;

    foreach (KeyValuePair<string, object> prop in request.Properties)
    {
        copyOfRequest.Properties.Add(prop);
    }

    foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
    {
        copyOfRequest.Headers.TryAddWithoutValidation(header.Key, header.Value);
    }

    return copyOfRequest;
}

此问题的另一个解决方案是在请求流上启用缓冲。 这是通过为 HttpRequest 调用 extension method EnableBuffering() 来完成的,例如

public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
    context.Request.EnableBuffering();
  ...

有关详细信息,请参阅 https://devblogs.microsoft.com/dotnet/re-reading-asp-net-core-request-bodies-with-enablebuffering/