β

Using ASP.NET Core 2.1's HttpClientFactory with Re

Scott Hanselman's Blog 156 阅读

Strong by Lucyb_22 used under Creative Commons from Flickr When I moved my podcast site over to ASP.NET Core 2.1 I also started using HttpClientFactory and wrote up my experience . It's a nice clean way to centralize both settings and policy for your HttpClients, especially if you're using a lot of them to talk to a lot of small services.

Last year I explored Refit, an automatic type-safe REST library for .NET Standard . It makes it super easy to just declare the shape of a client and its associated REST API with a C# interface:

public interface IGitHubApi

{

    [Get("/users/{user}")]

    Task<User> GetUser(string user);

}

and then ask for an HttpClient that speaks that API's shape, then call it. Fabulous.

var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");



var octocat = await gitHubApi.GetUser("octocat");

But! What does Refit look like and how does it work in an HttpClientFactory-enabled world? Refit has recently been updated with first class support for ASP.NET Core 2.1's HttpClientFactory with the Refit.HttpClientFactory package .

Since you'll want to centralize all your HttpClient configuration in your ConfigureServices method in Startup, Refit adds a nice extension method hanging off of Services.

You add a RefitClient of a type, then add whatever other IHttpClientBuilder methods you want afterwards:

services.AddRefitClient<IWebApi>()

        .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.example.com"));

        // Add additional IHttpClientBuilder chained methods as required here:

        // .AddHttpMessageHandler<MyHandler>()

        // .SetHandlerLifetime(TimeSpan.FromMinutes(2));

Of course, then you can just have your HttpClient automatically created and passed into the constructor. You'll see in this sample from their GitHub that you get an IWebAPI (that is, whatever type you want, like my IGitHubApi) and just go to town with a strongly typed interfaces of an HttpClient with autocomplete.

public class HomeController : Controller

{

    public HomeController(IWebApi webApi)

    {

        _webApi = webApi;

    }



    private readonly IWebApi _webApi;



    public async Task<IActionResult> Index(CancellationToken cancellationToken)

    {

        var thing = await _webApi.GetSomethingWeNeed(cancellationToken);



        return View(thing);

    }

}

Refit is easy to use, and even better with ASP.NET Core 2.1. Go get Refit and try it today !

* Strong image by Lucyb_22 used under Creative Commons from Flickr


Sponsor: Check out dotMemory Unit , a free unit testing framework for fighting all kinds of memory issues in your code. Extend your unit testing with the functionality of a memory profiler.



© 2018 Scott Hanselman. All rights reserved.
作者:Scott Hanselman's Blog
Scott Hanselman on Programming, User Experience, The Zen of Computers and Life in General
原文地址:Using ASP.NET Core 2.1's HttpClientFactory with Re, 感谢原作者分享。

发表评论