【发布时间】:2019-05-07 01:31:02
【问题描述】:
我的 HttpClientFactory 有问题,我正在尝试从 DI 向我的“SomeClient”注入一个 CancellationTokenSource,该“SomeClient”配置为:
services.AddHttpClient<ISomeClient, SomeClient>(a =>
a.BaseAddress = new Uri(address))
我在 AddScoped() 的 Startup.cs 中注入了 cancelTokenSource。
如果我将 CancellationTokenSource 添加到 SomeClient 构造函数,它会说
无法从根提供商解析范围服务“System.Threading.CancellationTokenSource”。
但如果我创建类似的东西:
services.AddScoped<ISomeClient, SomeClient>();
在构造函数中新建一个本地HttpClient,并注入CancellationTokenSource,一切都会好的。
所以我的问题是如何将 CancellationTokenSource 与 HttpClientFactory 一起使用?
【问题讨论】:
-
分享
SomeClient的代码以及你如何使用SomeClient。
标签: c# dependency-injection .net-core asp.net-core-2.0 httpclientfactory