One of the features that got released with ASP.NET Core 2.1 is HttpClientFactory. In this post let’s see how we can use HttpClientFactory in an ASP.NET Core 2.1 application.
There are basically three ways.
- HttpClientFactory directly
- Named Clients
- Typed Clients
Let’s have a look at them with sample use.
1. HttpClientFactory directly
Registration
services .AddHttpClient();
Use
public class ValuesController : ControllerBase { private readonly IHttpClientFactory httpClientFactory; public ValuesController(IHttpClientFactory httpClientFactory) { this.httpClientFactory = httpClientFactory; } [HttpGet] public async Task<ActionResult> Get() { var client = httpClientFactory.CreateClient(); client.BaseAddress = new Uri("http://myblog.blogspot.com"); // TODO: Client configuration string result = await client.GetStringAsync("/"); return Ok(result); } }
2. Named Clients
Registration
services .AddHttpClient() .AddHttpClient("BlogClient", client => { client.BaseAddress = new Uri("http://myblog.blogspot.com"); // TODO: Client configuration });
Use
public class ValuesController : ControllerBase { private readonly IHttpClientFactory httpClientFactory; public ValuesController(IHttpClientFactory httpClientFactory) { this.httpClientFactory = httpClientFactory; } [HttpGet] public async Task<ActionResult> Get() { var client = httpClientFactory.CreateClient("BlogClient"); string result = await client.GetStringAsync("/"); return Ok(result); } }
The only change is, instead of CreateClient(), I am calling CreateClient(clientName) which I have registered in ConfigureServices method.
3. Typed Clients
Consider the following implementation.
public class BlogClient { public HttpClient Client { get; set; } public BlogClient(HttpClient client) { client.BaseAddress = new Uri("http://myblog.blogspot.com"); // TODO: Client configuration this.Client = client; } }
Registration
services .AddHttpClient() .AddHttpClient("BlogClient", client => { client.BaseAddress = new Uri("http://myblog.blogspot.com"); // TODO: Client configuration }) .AddTypedClient<BlogClient>();
Use
public class ValuesController : ControllerBase { private readonly BlogClient blogClient; public ValuesController(BlogClient blogClient) { this.blogClient = blogClient; } [HttpGet] public async Task<ActionResult> Get() { string result = await this.blogClient.Client.GetStringAsync("/"); return Ok(result); } }
Alternatively, you can define a contract.
public interface IBlogClient { HttpClient Client { get; set; } } public class BlogClient : IBlogClient { public HttpClient Client { get; set; } public BlogClient(HttpClient client) { client.BaseAddress = new Uri("http://myblog.blogspot.com"); // TODO: Client configuration this.Client = client; } }
Registration
services .AddHttpClient() .AddHttpClient("BlogClient", client => { client.BaseAddress = new Uri("http://myblog.blogspot.com"); // TODO: Client configuration }) .AddTypedClient<BlogClient>() .AddTypedClient<IBlogClient, BlogClient>();
Use
public class ValuesController : ControllerBase { private readonly IBlogClient blogClient; public ValuesController(IBlogClient blogClient) { this.blogClient = blogClient; } [HttpGet] public async Task<ActionResult> Get() { string result = await this.blogClient.Client.GetStringAsync("/"); return Ok(result); } }
Hope this helps.
Additional Resources,
ASP.NET Core 2.1-preview1: Introducing HTTPClient factory - ASP.NET Blog
HttpClientFactory in ASP.NET Core 2.1 (Part 1) : Steve Gordon
HttpClientFactory for typed HttpClient instances in ASP.NET Core 2.1 - Scott Hanselman
Additional Resources,
ASP.NET Core 2.1-preview1: Introducing HTTPClient factory - ASP.NET Blog
HttpClientFactory in ASP.NET Core 2.1 (Part 1) : Steve Gordon
HttpClientFactory for typed HttpClient instances in ASP.NET Core 2.1 - Scott Hanselman
Happy Coding.
Regards.
Jaliya
No comments:
Post a Comment