简体   繁体   中英

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://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

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}");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM