最近博客园被**了, 赶紧水一文支持一下博客园,加油!
问题现象
当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;
}