![Strong by Lucyb_22 used under Creative Commons from Flickr Strong by Lucyb_22 used under Creative Commons from Flickr](https://img-blog.csdnimg.cn/img_convert/628d9032b3fe20cca55d917934553d8b.png)
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
翻译自: https://www.hanselman.com/blog/using-aspnet-core-21s-httpclientfactory-with-refits-rest-library