Blazor WASM 应用程序 HttpClient 中的 Polly 超时

问题描述 投票:0回答:1

.Net 和 Blazor 新手。我正在尝试延长 Blazor 应用程序中 HTTPClient 调用的超时(我假设)。无论我做什么,10 秒后都会发生超时并启动重试,这在调试时可能会很烦人。我认为这是 HTTPClient 超时,但无论我尝试什么,它似乎都会在 10 秒后超时。我在HTTP调用之前检查了超时,似乎是我设置的时间,但仍然超时。我是否错误地设置了 HTTP 超时,或者 Blazor 中的某些内容超时了?

在 Blazor 中设置 HTTP 客户端超时的代码

program.cs

var builder = WebApplication.CreateBuilder(args);

// Add service defaults & Aspire components.
builder.AddServiceDefaults();

// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
builder.Services.AddOutputCache();
builder.Services.ConfigureApplicationCookie(ops =>
{
    ops.ExpireTimeSpan = TimeSpan.FromMinutes(30);
    ops.SlidingExpiration = true;
});

builder.Services.AddHttpClient<ModelApiClient>(client =>
{
    // This URL uses "https+http://" to indicate HTTPS is preferred over HTTP.
    // Learn more about service discovery scheme resolution at https://aka.ms/dotnet/sdschemes.
    client.BaseAddress = new("https+http://myapipath/");
    client.Timeout = TimeSpan.FromMinutes(10);
}).SetHandlerLifetime(TimeSpan.FromMinutes(10));

Extensions.cs
(创建解决方案时自动生成)中,我有:

public static class Extensions
{
    public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder)
    {
        builder.ConfigureOpenTelemetry();

        builder.AddDefaultHealthChecks();

        builder.Services.AddServiceDiscovery();

        builder.Services.ConfigureHttpClientDefaults(http =>
        {
            // Turn on resilience by default
            http.AddStandardResilienceHandler();

            // Turn on service discovery by default
            http.AddServiceDiscovery();
        });

        return builder;
    }

razor 文件中的实际调用如下所示:

 Debug.WriteLine("Timeout: " + httpClient.Timeout.TotalMinutes);
 var response = await httpClient.PostAsJsonAsync("/api/whatever", singleModel); 

但呼叫总是因以下错误而停止:

Polly: Warning: Execution attempt. Source: '-standard//Standard-Retry', Operation Key: '', Result: 'The operation didn't complete within the allowed timeout of '00:00:10'.', Handled: 'True', Attempt: '0', Execution Time: 11134.0462ms

Polly.Timeout.TimeoutRejectedException: The operation didn't complete within the allowed timeout of '00:00:10'.
 ---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
 ---> System.IO.IOException: Unable to read data from the transport connection: The I/O operation has been aborted because of either a thread exit or an application request..
 ---> System.Net.Sockets.SocketException (995): The I/O operation has been aborted because of either a thread exit or an application request.
   --- End of inner exception stack trace ---

这是 HTTPClient 问题还是其他问题?

尝试将 HTTPClient 调用的生命周期延长到 10 分钟,但 10 秒后仍然取消。

.net blazor timeout dotnet-httpclient polly
1个回答
0
投票

AddStandardResilienceHandler
扩展注册了一系列弹性策略。您可以在这篇 .NET 博客文章中阅读相关内容。其中一种策略是针对每个请求强制执行的尝试超时。此超时的默认值为 10 秒。您可以像这样覆盖该值:

.AddStandardResilienceHandler(options =>
{
    options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(60);
});

或者,如果您不想在调试期间使用这些恢复策略,则只需注释掉

http.AddStandardResilienceHandler();
行即可。

© www.soinside.com 2019 - 2024. All rights reserved.