将ASP.NET Core 2.1的HttpClientFactory与Refit的REST库一起使用

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.

当我将播客站点移至ASP.NET Core 2.1时,我也开始使用HttpClientFactory并写下了自己的经验。 这是一种集中设置HttpClient的设置和策略的好方法,尤其是当您使用大量设置与许多小型服务进行通信时。

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:

去年,我探索了Refit,这是一个用于.NET Standard的自动类型安全REST库。 使用C#接口声明客户端及其关联的REST API的形状非常容易:

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.

然后要求一个说明该API形状的HttpClient,然后调用它。 极好。

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.

但! Refit的外观如何以及在启用HttpClientFactory的世界中如何工作? Refit最近通过Refit.HttpClientFactory包对ASP.NET Core 2.1的HttpClientFactory的一流支持进行更新

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.

由于您希望将所有HttpClient配置集中在Startup的ConfigureServices方法中,因此Refit添加了一个很好的扩展方法,该方法与Services无关。

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

您添加类型的RefitClient,然后添加您想要的其他任何IHttpClientBuilder方法:

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.

当然,那么您就可以自动创建HttpClient并将其传递到构造函数中。 您将从他们的GitHub的此示例中看到,您获得了IWebAPI(即您想要的任何类型,例如IGitHubApi),并且带着带有自动完成功能的HttpClient的强类型接口进入城镇。

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!

调整易于使用,并且使用ASP.NET Core 2.1甚至更好。 去改装,今天就尝试

* Strong image by Lucyb_22 used under Creative Commons from Flickr

* Lucyb_22在Flickr的知识共享中使用的强图像

翻译自: https://www.hanselman.com/blog/using-aspnet-core-21s-httpclientfactory-with-refits-rest-library

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值