为重复使用的HttpClient对象动态修改Timeout

最近博客园被**了, 赶紧水一文支持一下博客园,加油!

问题现象

HttpClient被使用过之后, 在修改它们的属性会抛出错误This instance has already started one or more requests. Properties can only be modified before sending the first request.

场景

  • 单例 HttpClient 对象, 我们要修改它的 Timeout
  • Scope生命周期的 HttpClient, 我们要修改它的 Timeout

解决方法一

可以把 HttpClient 的生命周期改成 Transient, 并且每次要用的时候都从 IServiceProvider 获取.

解决方法二

  • 在注册 HttpClient 的时候把它的 Timeout 修改为 System.Threading.Timeout.InfiniteTimeSpan
    services.AddHttpClient<MyApiService>((sp, client) =>
    {
        client.Timeout = System.Threading.Timeout.InfiniteTimeSpan;
        client.BaseAddress = sp.GetService<MyOptions>().ApiEndpoint;
        }
    });
    
  • 然后调用的地方使用自己的CancellationToken来实现即可, 其实HttpClient.Timeout在内部也是一样的方式.
    async Task<HttpResponseMessage> RequestAsync(HttpRequestMessage httpRequest, CancellationToken token, TimeSpan timeout)
    {
        using var cts = CancellationTokenSource.CreateLinkedTokenSource(token);
        cts.CancelAfter(timeout);
        return await this.httpClient.SendAsync(httpRequest, cts.Token);
    }
    

参考HttpClient.SendAsync的部分源代码:

CancellationTokenSource cts;
bool disposeCts;
bool hasTimeout = _timeout != s_infiniteTimeout;
if (hasTimeout || cancellationToken.CanBeCanceled)
{
    disposeCts = true;
    cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _pendingRequestsCts.Token);
    if (hasTimeout)
    {
        cts.CancelAfter(_timeout);
    }
}
else
{
    disposeCts = false;
    cts = _pendingRequestsCts;
}

posted @ 2021-04-02 18:36  czd890  阅读(737)  评论(0编辑  收藏  举报