unable to make httpclientfactory working from a .net framework 4.7.2 winform project

Viewed 70

i read about everything i could find about httpclientfactory and after a few days strugling 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 winform 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 serviceProvider As New ServiceCollection

        '
        'reference the policyholder class that contians all my Polly policies
        Dim PolHolder As New PollySharingPoliciesByDI.PolicyHolder
        '
        '--------Registering and injecting HttpClients (by Name)
        serviceProvider.AddHttpClient("s2sfHTTPClient", Sub(c)
                                                            'c.BaseAddress = New Uri("https://api.google.com/")
                                                            c.DefaultRequestHeaders.Add("Accept", "application/json")
                                                        End Sub).AddPolicyHandler(PolHolder.httpRetryWithReauthorizationPolicy())
        serviceProvider.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)

        serviceProvider.AddHttpClient(Of TypedCustomHTTPClient)().SetHandlerLifetime(TimeSpan.FromMinutes(5)).AddPolicyHandler(PolHolder.httpRetryWithReauthorizationPolicy()) 'Set lifetime to five minutes
        Debug.WriteLine("")
        '


        '
        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?

0 Answers
Related