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://docs.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
NET to version 6. You can do that by updating to the new shiny Visual Studio 2022 with built-in SDK or downloading it from here: . NET 6.0 SDK. In Visual Studio 2022, Azure Functions Core Tools, necessary for developing Azure Functions, are available by adding the Azure Cloud Tool in the installation process of VS.
NET Core 3.1 and . NET 5 Azure Functions are. As of March 2021, Microsoft announced that Azure Functions are supported running on . NET 5.
NET Core 2.1 introduced two approaches, one of them being IHttpClientFactory. It's an interface that's used to configure and create HttpClient instances in an app through Dependency Injection (DI). It also provides extensions for Polly-based middleware to take advantage of delegating handlers in HttpClient.
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();
}
}
}
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}");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With