i read about everything i could find about HttpClientFactory
and after a few days struggling with this, i am about to give up, but taking a chance here in case anyone could help. I am just trying to implement HttpClientFactory
in my .net 4.7.2 Winforms app for a rest-api client.
I tried implementing both Typed and Named client, but i am getting a null reference each time i am trying to instantiate it in my code. SO here what i did so far:
For the Typed Client, i created a class:
Imports System.Net.Http
Imports Microsoft.Extensions.Http
Imports Microsoft.Extensions.DependencyInjection
Public Class TypedCustomHTTPClient
Public Property _Client() As HttpClient
Public Sub New(ByVal httpClient As HttpClient)
'httpClient.BaseAddress = New Uri("https://api.google.com/")
httpClient.DefaultRequestHeaders.Add("Accept", "application/json")
httpClient.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample")
_Client = httpClient
End Sub
End Class
then my Project Main Sub i am registering my Typed Client an also a Named client (i am not sure that i am doing it the correct way)
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.Extensions.Hosting
Imports Microsoft.Extensions.Http
Imports Polly
Module MainModule
Public Sub Main()
Dim seviceColl As New ServiceCollection
'
'--------Registering and injecting HttpClients (by Name)
seviceColl.AddHttpClient("s2sfHTTPClient", Sub(c)
'c.BaseAddress = New Uri("https://api.google.com/")
c.DefaultRequestHeaders.Add("Accept", "application/json")
End Sub).AddPolicyHandler(PolHolder.httpRetryWithReauthorizationPolicy())
seviceColl.AddHttpClient("GitHub", Sub(httpClient)
httpClient.BaseAddress = New Uri("https://api.github.com/")
' using Microsoft.Net.Http.Headers;
' The GitHub API requires two headers.
''httpClient.DefaultRequestHeaders.Add(HeaderNames.Accept, "application/vnd.github.v3+json")
''httpClient.DefaultRequestHeaders.Add(HeaderNames.UserAgent, "HttpRequestsSample")
End Sub)
'Registering and injecting HttpClients (by Type)
seviceColl.AddHttpClient(Of TypedCustomHTTPClient)().SetHandlerLifetime(TimeSpan.FromMinutes(5)).AddPolicyHandler(PolHolder.httpRetryWithReauthorizationPolicy()) 'Set lifetime to five minutes
'Building Service Provider
Dim serviceprovider = servicecoll.BuildServiceProvider
'
'
'
Application.Run(New Form1()) ''//Use your main form here
End Sub
End Module
when i try to either use the typed client or the named client (with the .CreateClient
method as it should) i am getting a Null reference error on the CreateClient
Line.
Private Property _httpClientFactory As IHttpClientFactory
Public Function TestQuery2(ByVal soqlQuery As String) As String
Dim customclient = _httpClientFactory.CreateClient("s2sfHTTPClient")
'Using customclient._Client '= New HttpClient()
'
Dim restRequest As String = InstanceUrl + API_ENDPOINT & "query/?q=" & soqlQuery
Dim request = New HttpRequestMessage(HttpMethod.[Get], restRequest)
request.Headers.Add("Authorization", "Bearer " & AuthToken)
request.Headers.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
request.Headers.Add("X-PrettyPrint", "1")
Dim response = customclient.SendAsync(request).Result
Return response.Content.ReadAsStringAsync().Result
'End Using
End Function
Any idea? what am i doing wrong?