Azure Function EventHubTrigger 如何从 Azure App 配置中读取 EventHubName 和连接字符串?

Azure Function EventHubTrigger how to read EventHubName and Connection string from Azure App Configuration?

我正在尝试从 Azure 应用程序配置中读取 eventhubname 和连接字符串,而不是函数应用程序设置,但我无法让它工作,如果我从函数应用程序本身来看它工作正常但我需要从应用程序配置集中配置存储。

这是我到目前为止的小功能

  public class CDSSoftDelete
    {
        static string _eventHubname = null;
        string _connectionString;
        private readonly IConfiguration _config;

        public CDSSoftDelete(IConfiguration config, IConfigurationRefresher configurationRefresher)
        {
            if (config == null) throw new ArgumentNullException(nameof(config));
            if (configurationRefresher == null) throw new ArgumentNullException(nameof(configurationRefresher));

            configurationRefresher.TryRefreshAsync();

            _config = config;

            _eventHubname = config["SLQueueManager:Settings:EventHubName"];
            _connectionString = config["SLQueueManager:Settings:EventHubConnectionString"];
        }

        [FunctionName("CDSSoftDelete")]
        public async Task Run([EventHubTrigger(_config["SLQueueManager:Settings:EventHubName"], Connection = _connectionString)] EventData[] events, ILogger log)
        {
           
        }
    }

但这行不通,因为 _config 变量没有对象引用,所以有点麻烦 22

如何正确读取这些配置设置?

使用此代码:

Environment.GetEnvironmentVariable("something");

下面是一个示例,说明如何从 Azure 应用程序配置中获取队列名称并将其用于 QueueTrigger。您应该能够为 EventHubTrigger 执行类似的操作。它使用应用程序设置绑定表达式。请注意,由于 Azure Functions 的限制,消费计划不支持此功能。

https://github.com/Azure/AppConfiguration/blob/main/examples/DotNetCore/AzureFunction/FunctionApp/ReadQueuedMessage.cs

您需要使用 dependency injection 并添加 Azure App Configuration 作为额外的配置源,以便您的应用功能与之对话。

你可以按照quick start guideline在你的启动注册他们。