dotnet (.Net) Core在不是Web 项目中使用HttpClientFactory正确用法
在不是web 项目中也不是Api 中使用 HttpClientFactory
先需要添加
Microsoft.Extensions.DependencyInjection.Abstractions
Microsoft.Extensions.Http
static readonly IServiceCollection _services = new ServiceCollection();
public static void AddServerHttpClient() { _services.AddHttpClient(); }
public static HttpClient GetServerHttpClient() { IHttpClientFactory httpClientFactory= _services.BuildServiceProvider().GetService<IHttpClientFactory>(); return httpClientFactory.CreateClient(); }