Using HttpClient on Azure Functions


HttpClient use a socket-line resource of your server. So, you don’t make many HttpClient instance. If you can share instance for HttpClient. It’s the same also Azure Functions.


  • .NET Core v3.1
  • Azure Functions v3
  • Microsoft.NET.Sdk.Functions v3.0.3
  • Microsoft.Azure.Functions.Extensions v1.1.0
  • Microsoft.Extension.Http v3.1.20
  • Visual Studio for Mac v8.10
    • Azure Functions Core Tools v3.0.3873
  • C# v8
  • macOS v11.6


Install Microsoft.Extension.Http via NuGet, then add builder.Services.AddHttpClient() to your code.

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

[assembly: FunctionsStartup(typeof(YourNameSpace.Startup))]

namespace YourNameSpace
    public class Startup : FunctionsStartup
        public override void Configure(IFunctionsHostBuilder builder)

Next, add Functions class. Example is here:

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace YourNameSpace
    public class Package
        private readonly HttpClient _client;

        public Package(IHttpClientFactory httpClientFactory)
            this._client = httpClientFactory.CreateClient();

        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
            log.LogInformation("C# HTTP trigger function processed a request.");

            var response = await _client.GetAsync("");            
            string responseMessage = $"Got response, http status is {response.StatusCode}";
            return new OkObjectResult(responseMessage);