In a .NET Framework project without dependency injection, is it better to get an HttpClient instance through its constructor (new HttpClient) or through IHttpClientFactory? Which of the following two cases is better?
- HttpClient constructor
Public Shared Function GetHttpClient() As HttpClient
Return new HttpClient
End Function
- IHttpClientFactory
Private Shared mHttpClientFactory As IHttpClientFactory
Shared Sub New()
Dim serviceProvider = New ServiceCollection().AddHttpClient().BuildServiceProvider()
mHttpClientFactory = serviceProvider.GetService(Of IHttpClientFactory)
End Sub
Public Shared Function GetHttpClient() As HttpClient
Return mHttpClientFactory.CreateClient
End Function
As I have read, it is recommended to reuse the HttpClient instance created in case 1. Shall I reuse the HttpClient instance created in case 2, or may I repeatedly call GetHttpClient, without reusing the instance returned?