IHttpClientFactory 的解析服务在 Azure 函数上失败

Resolving service for IHttpClientFactory fails on Azure Function

我正在尝试在 Azure Function v3 上注入 IHttpClientFactory 服务,但我不断收到以下错误消息,提示服务解析失败。 我在本地使用 azure-functions-core-tools@v3 运行 Azure Function。

[2/14/2020 5:45:19 PM] Executed 'Foo' (Failed, Id=24489b3b-af99-417e-b175-443b76c241d5)
[2/14/2020 5:45:19 PM] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'System.Net.Http.IHttpClientFactory' while attempting to activate 'MyFunction.Function.Foo'.

我有一家初创公司 class,它应该为 IHttpClientFactory 注入服务。

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

namespace MyFunction
{
    public class Startup : FunctionsStartup
    {

        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
        }
    }
}

下面是一个 azure 函数 class,它使用 IHttpClientFactory 的注入服务来创建 HTTP 客户端并向服务器发送 GET 请求。

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

namespace MyFunction.Function
{
    public class Foo
    {
        private readonly HttpClient httpClient;
        public Scrape(IHttpClientFactory httpClientFactory)
        {
            httpClient = httpClientFactory.CreateClient();
        }

        [FunctionName("Foo")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var result = await httpClient.GetAsync("https://google.com");
            var data = await result.Content.ReadAsStringAsync();
            return new OkObjectResult(data);
        }
    }
}

我是不是漏掉了什么?

您的 startup.cs class 似乎缺少程序集参考?

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

尝试将其添加到启动程序中 class。在顶部的 using 语句之后和任何名称空间声明之前添加它。

此外,我见过的大多数示例都显示了在函数执行期间实际创建的客户端,而不是在默认构造函数中创建的。

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

namespace MyFunction.Function
{
    public class Foo
    {
        private readonly HttpClient httpClient;
        public Scrape(IHttpClientFactory httpClientFactory)
        {
            factory = httpClientFactory;
        }

        [FunctionName("Foo")]
        public async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            HttpClient httpClient = factory.CreateClient();
            var result = await httpClient.GetAsync("https://google.com");
            var data = await result.Content.ReadAsStringAsync();
            return new OkObjectResult(data);
        }
    }
}