当使用推荐的AddHttpClient
扩展来配置IHttpClientFactory
并包含一些默认HttpClient
设置时,我为该属性设置的值在我随后创建Timeout
的实例中看不到。HttpClient
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