Quote : "You're (probably still) using HttpClient wrong and it is destabilizing your software" from Josef

artydev

artydev

Posted on May 19, 2020

Quote : "You're (probably still) using HttpClient wrong and it is destabilizing your software" from Josef

I have tried to use IHTTPClient in my .net core MVC app

In Startup.cs

services.AddHttpClient("Visites", configureClient: client => {
    client.BaseAddress = new Uri("http://xx-yy.abcd/visiteapi/visites/");
    client.DefaultRequestHeaders.Add(name: "Accept", value: "application/json");

}).ConfigurePrimaryHttpMessageHandler(() =>
{
    return new HttpClientHandler()
    {
        UseDefaultCredentials = true,
    };
});

In VisitesController.cs

public async Task<ActionResult> LastVisites()
{
    var httpClient = _httpClient.CreateClient("Visites");
    var data = await httpClient.GetStringAsync("lastvisites?agIdent=3631&nbVisites=5");
    IList<Visite> lastVisites= DeserializeObject<IList<Visite>>(data);
    return View(lastVisites); ;
}

Here is a very nice article from Josef explaining why use IHTTPClient

Using HttpClient

đź’– đź’Ş đź™… đźš©
artydev
artydev

Posted on May 19, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related