Код для установки тайм-аута HTTP-клиента в программе Blazor.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(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));
Код: Выделить всё
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;
}
Код: Выделить всё
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 до 10 минут, но все равно был отменен через 10 секунд. .
Подробнее здесь: https://stackoverflow.com/questions/790 ... httpclient