Pingpong
Pingpong

Reputation: 8009

Availability of HttpClientFactory for Azure Functions v2

I want to know if HttpClientFactory or similar is available for Azure Functions v2.

Below is what is recommended, but HttpClientFactory or similar is not shown.

// Create a single, static HttpClient
private static HttpClient httpClient = new HttpClient();

public static async Task Run(string input)
{
    var response = await httpClient.GetAsync("https://example.com");
    // Rest of function
}

https://learn.microsoft.com/en-gb/azure/azure-functions/manage-connections

Below is a good link but I am not sure if it can be used on production, or an official feature is available.

https://www.tpeczek.com/2018/12/alternative-approach-to-httpclient-in.html

Update:

Problem to solve

1 Providing managed HttpClient pool instead of single HttpClient, like HttpClientFactory in ASP.NET CORE 2.2

Upvotes: 14

Views: 12066

Answers (1)

Kzryzstof
Kzryzstof

Reputation: 8382

Update

Since the original answer has been posted, the Azure Functions have been updated and there is a new FunctionStartup class to use instead of IWebJobsStartup:

Note: You will first need to install the Microsoft.Extensions.Http NuGet package

using MyNamespace.Functions;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

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

Original Answer

Using the latest Azure Function v2 runtime, IHttpClientFactory is indeed available to you since the Azure Function v2 runtime has been moved to ASP.Net Core 2.2:

Release v2.0.12265

First, you can provide an implementation for IWebJobsStartup where you will define what services to inject.

Add a reference to the NuGet package Microsoft.Extensions.Http and use the extension method AddHttpClient() so that the HttpClient instance your Azure Functions will receive will come from an IHttpClientFactory.

using MyNamespace.Functions;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;
using Microsoft.Extensions.DependencyInjection;

[assembly: WebJobsStartup(typeof(Startup))]
namespace MyNamespace.Functions
{
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            builder.Services.AddHttpClient();
        }
    }
}

You can then update your Azure Function by removing the static keywords and add a constructor to enable the injection of the instance of HttpClient built by the internal -I think- DefaultHttpClientFactory instance:

public sealed class MyFunction()
{
    private readonly HttpClient _httpClient;

    public MyFunction(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public void Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/resource/{resourceId}")] HttpRequest httpRequest, string resourceId)
    {
         return OkObjectResult($"Found resource {resourceId}");
    }
}

Upvotes: 25

Related Questions