带有EventGridTrigger的Azure Function阻止运行FunctionsStartup类的Configure方法

时间:2019-06-24 15:51:23

标签: azure asp.net-core dependency-injection azure-functions azure-eventgrid

我正在尝试一个函数,该函数的依赖项已按文档Use dependency injection in .NET Azure Functions中所述注入。我的启动类定义为:

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

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

    namespace MyFunctions{    
      public class Startup : FunctionsStartup
      {
        public override void Configure(IFunctionsHostBuilder builder)
        {
          builder.Services.AddHttpClient();
        }
      }
    }

我在builder.Services.AddHttpClient()语句上设置了一个断点,以确保配置了DI。 然后,我使用HttpTrigger定义函数:

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

    namespace MyFunctions
    {
        public class ChangeProducer
        {
            private readonly HttpClient _httpClient;

            public ChangeProducer(IHttpClientFactory httpClientFactory)
            {
                _httpClient = httpClientFactory.CreateClient();
            }

            [FunctionName("ChangeProducer")]
            public void Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Reservation")]HttpRequest request, ILogger log)
            {
                Console.WriteLine("foo");
            }
        }
    }

当我从Visual Studio运行此程序时,我在Startup.Configure中遇到了断点。 很棒!

然后我将函数更改为使用EventGridTrigger

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

    namespace MyFunctions
    {
        public class ChangeProducer
        {
            private readonly HttpClient _httpClient;

            public ChangeProducer(IHttpClientFactory httpClientFactory)
            {
                _httpClient = httpClientFactory.CreateClient();
            }

            [FunctionName("ChangeProducer")]
            public void Run([EventGridTrigger]EventGridEvent eventGridEvent, ILogger log)
            {
                Console.WriteLine("foo");
            }
        }
    }

一旦做出更改,并且没有其他更改,我 不要命中 Startup.Configure中的断点。另外,我知道DI失败了,因为当我尝试调用该函数时,我收到一条错误消息,内容为:

    Executed 'ChangeProducer' (Failed, Id=06ae8b88-07c4-4150-91e5-8b88400aed72)
    Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'System.Net.Http.IHttpClientFactory' while attempting to activate 'MyFunctions.ChangeProducer'.

是否存在已知问题?我不知道这个。唯一的区别是触发器类型。

更新2019-06-24-仅仅是依赖注入

我想弄清楚问题是依赖项注入不起作用,当未注入对EventGridTrigger的依赖项时,HttpClient没有问题。将构造函数更改为以下内容,并在由EventGridTrigger触发时,该函数可以正常工作:

    public ChangeProducer()
    {
        _httpClient = new HttpClient();
    }

2 个答案:

答案 0 :(得分:0)

我已成功使用以下程序包和Visual Studio 2019 16.1.3版进行了测试。

enter image description here

以下屏幕片段显示了调试步骤:

  1. 在启动阶段

enter image description here

  1. 通过POST调用功能

    http://localhost:7071/runtime/webhooks/EventGrid?functionName=Function1

enter image description here

enter image description here

答案 1 :(得分:0)

在创建一个新的功能应用程序并慢慢构建它并与遇到问题的项目进行比较之后,我已经弄清了依赖注入问题。 host.json文件包含一个名为extensionBundle的属性。删除之后,依赖项注入将再次开始工作。整个属性如下所示:

    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    }