对于AddHttpClient
,它将注册SomeClient
为Transient
。但是您注册CancellationTokenSource
为Scoped
. 这是造成的根源。
HttpClientFactoryServiceCollectionExtensions.cs
public static IHttpClientBuilder AddHttpClient<TClient>(this IServiceCollection services)
where TClient : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
AddHttpClient(services);
var name = TypeNameHelper.GetTypeDisplayName(typeof(TClient), fullName: false);
var builder = new DefaultHttpClientBuilder(services, name);
builder.AddTypedClient<TClient>();
return builder;
}
HttpClientBuilderExtensions
public static IHttpClientBuilder AddTypedClient<TClient>(this IHttpClientBuilder builder)
where TClient : class
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.Services.AddTransient<TClient>(s =>
{
var httpClientFactory = s.GetRequiredService<IHttpClientFactory>();
var httpClient = httpClientFactory.CreateClient(builder.Name);
var typedClientFactory = s.GetRequiredService<ITypedHttpClientFactory<TClient>>();
return typedClientFactory.CreateClient(httpClient);
});
return builder;
}
因此,您可以尝试注册CancellationTokenSource
为Transient
.
services.AddTransient<CancellationTokenSource>();