【发布时间】:2020-02-10 21:30:43
【问题描述】:
我想使用新的HttpClientFactory,但设置时遇到问题。
我有以下几点(我只是把点头的例子放在一起来解释我的观点)
public class MyGitHubClient
{
public MyGitHubClient(HttpClient client)
{
Client = client;
}
public HttpClient Client { get; }
}
然后在我的webapi.Startup我有
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<MyGitHubClient>(client =>
{
client.BaseAddress = new Uri("https://api.github.com/");
//etc..
});
//NOW I get error "Class name is not valid at this point" for "MyGitHubClient" below
services.AddSingleton<IThirdPartyService>(x => new ThirdPartyService(MyGitHubClient,someOtherParamHere));
///etc...
}
第三方构造函数
public ThirdPartyService(HttpClient httpClient, string anotherParm)
{
}
当我必须调用一个我无法控制的类时,如何使用HttpClientFactory?
【问题讨论】:
标签: c# asp.net-core asp.net-core-webapi asp.net-core-2.1 httpclientfactory