Azure Function App IHttpClientFactory Implementation

In this post, I am going to implement IHttpClientFactory to the Azure Function App and inject it to the sample function. To be able to do this, the Azure Function App should have a Startup.cs file. If your app does not have a Startup.cs file, you can use the sample from this article.

First of all, I would like to explain why we should use the IHttpClientFactory interface instead of the HttpClient class. The IHttpClientFactory interface has a few benefits for the application. Some of these are;

  • It allows you to create a central HttpClient object in the Startup.cs file. You can use this object and properties for all requests and it is easy to modify.
  • It manages object lifetime. It means, you do not need to follow all your requests and dispose of them at the end of the process. IHttpClientFactory does this for you. Also with this ability, the application can avoid DNS problems as well.

Before starting to code, I must add some libraries for the IHttpClientFactory interface. These libraries are;

Install-Package Microsoft.Extensions.Http
Install-Package Microsoft.Azure.Functions.Extensions
Install-Package Microsoft.NET.Sdk.Functions

Those libraries can be added via NuGet. After adding these libraries, the app is ready to implement the IHttpClientFactory interface. As a first coding step, I must add a Startup.cs file to the function app. (If you have, you can skip this step.) The Startup.cs file should be in the application’s root and basically, it is like the following sample. If you have any other services, you have to add it to the Startup.cs file as well.

[assembly: FunctionsStartup(typeof(Application.Namespace.Startup))]
public class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        ....
        builder.Services.AddHttpClient();
        ....
    }
}

Note: You have to change Application.Namespace. with your root namespace.

Now, the app is ready to use the IHttpClientFactory interface in functions. To be able to use this, I am going to use the constructor injection.

public YourFunctionName(IHttpClientFactory httpClientFactory)
{
    ...
    _httpClientFactory = httpClientFactory;
    ...
}

To define a named HttpClient object, you can use the following sample in the startup.cs file. (Benefit number 1)

builder.Services.AddHttpClient("cinarr", c =>
{
    c.BaseAddress = new Uri("https://cinarr.com/");
    c.DefaultRequestHeaders.Add("Cache-Control", "max-age=0");
    c.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
});

To use this named HttpClient object;

[FunctionName("SampleFunction")]
public async Task<IActionResult> Run(
   [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "samplefunct")]
   HttpRequest req,
   ILogger log)
{
   ....
   var httpClient = _httpClientFactory.CreateClient("cinarr");
   ....
}

Note: If you are using a static function, you have to change it to instance. It means the function must be like the following.

[FunctionName("SampleFunction")]
public async Task<IActionResult> Run(
   [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "samplefunct")]
   HttpRequest req,
   ILogger log)
{
   ....
}

I tried the basic implementation for the IHttpClientFactory interface. I hope this article can be helpful for HttpClient problems. To add any additional information or any mistake you can contact with me via this post or article[at]cinarr[dot]com

See you in the next posts 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *