我不禁要问,我是否在我的网络中执行过:
<>Startup.cs => Configureservices
services.AddHttpClient("MyHttpClient")
<>Startup.cs => Configure
applicationLifetime.ApplicationStopping.Register(() => {
MyController.httpClient.Dispose();
});
<><><><><>><>>>><>>>>>>
private static IHttpClientFactory _httpClientFactory;
public static HttpClient httpClient = null;
public MyController(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
if (httpClient == null)
{
httpClient = _httpClientFactory.CreateClient("MyHttpClient");
}
}
<>Methods
[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
public async void MyMethod1() {
await httpClient.SendAsync()
....
}
[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
public async void MyMethod2() {
await httpClient.SendAsync()
....
}