0

In my c# console app (.net 4.6.1), I am required make API calls (Get/Post/Put/delete) etc to random endpoints. The details of the url, payload etc comes from a database table. The console app gets the httpClientFactory instance as shown below

var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();

For each record I get from the table, I call a method to make an API call. To that method I pass the httpClientFactory. In that method, I am creating and using HttpClient as follows.

var httpClient = httpClientFactory.CreateClient();
//set headers
//make api call, for ex  var response = await httpClient.PutAsync(apiTask.URL, content);
//Return resposne

I am no where disposing of httpClient. As I see the memory usage, it keeps on increasing and I don't see it going down even after hours.

I followed the following article https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.1

Am i using it right?. Should I be using the "using(httpClient)"

user738338
  • 33
  • 1
  • 8

1 Answers1

0

There is no need to instantiate new HttpClient per request, you can use share one instance between all of them since HttpClient is designed to be reusable and thread safe.

check this, this, and this, it is also woth to look at this

Saeed Ganji
  • 197
  • 17