gpt4 book ai didi

.net-core - AddHttpClient 配置的 HttpClient.Timeout 被忽略

转载 作者:行者123 更新时间:2023-12-05 00:47:23 27 4
gpt4 key购买 nike

当使用推荐的 AddHttpClient 扩展来配置 IHttpClientFactory 并包括一些默认的 HttpClient 设置时,我为 设置的值在我随后创建的 HttpClient 实例中看不到 Timeout 属性。

Demo 使用 Azure Function,类似于 ASP.NET Core。

public class Startup : FunctionsStartup
{
public static readonly int PushTimeoutMs = 3000;

public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient("PushNotification", client =>
{
client.Timeout = TimeSpan.FromMilliseconds(PushTimeoutMs);
});
}
}

创建客户端。

public class TestFunctionTwo
{
public TestFunctionTwo(IHttpClientFactory httpClientFactory)
{
this.HttpClientFactory = httpClientFactory;
}

//

public IHttpClientFactory HttpClientFactory { get; }

//

[FunctionName("TestFunctionTwo")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
await Task.Delay(100);

return new OkObjectResult($"OK - Timeout: {this.HttpClientFactory.CreateClient().Timeout.TotalMilliseconds}");
}
}

返回

OK - Timeout: 100000

最佳答案

这是因为您需要创建一个与您配置的名称相同的 HttpClient,即 "PushNotification"

return new OkObjectResult($"OK - Timeout: {this.HttpClientFactory.CreateClient("PushNotification").Timeout.TotalMilliseconds}");

关于.net-core - AddHttpClient 配置的 HttpClient.Timeout 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57399056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com