Thursday, June 7, 2018

ASP.NET Core 2.1: HttpClientFactory

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.
  1. HttpClientFactory directly
  2. Named Clients
  3. 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);
    }
}
Happy Coding.

Regards.
Jaliya

No comments:

Post a Comment