简体   繁体   English

Azure Function EventHubTrigger 如何从 Azure App Configuration 中读取 EventHubName 和 Connection 字符串?

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

I am trying to read the eventhubname and connection string from Azure App Configuration, NOT function app settings, but I cannot get this to work, if I real from the function app itself it works fine but I need to read from App Configuration centralized configuration store. I am trying to read the eventhubname and connection string from Azure App Configuration, NOT function app settings, but I cannot get this to work, if I real from the function app itself it works fine but I need to read from App Configuration centralized configuration store .

Here is my small function so far到目前为止,这是我的小 function

  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)
        {
           
        }
    }

But this does not work because the _config variable does not have an object reference, so its a bit of a catch 22但这不起作用,因为 _config 变量没有 object 引用,所以它有点问题 22

How can I read those config settings correctly?如何正确读取这些配置设置?

Use this code:使用此代码:

Environment.GetEnvironmentVariable("something");

Here is an example of how to get queue name from Azure App Configuration and use it for QueueTrigger.这是一个如何从 Azure 应用程序配置中获取队列名称并将其用于 QueueTrigger 的示例。 You should be able to do something similar for EventHubTrigger.您应该能够为 EventHubTrigger 做类似的事情。 It uses app setting binding expression.它使用应用程序设置绑定表达式。 Please note that this is not supported in the consumption plan due to limitations in Azure Functions.请注意,由于 Azure 功能的限制,消费计划不支持此功能。

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

You need to use dependency injection and add Azure App Configuration as extra configuration source in order for your app function talk with it.您需要使用dependency injection并添加Azure App Configuration作为额外的配置源,以便您的应用程序 function 与之对话。

you can follow the quick start guideline in your startup register them.您可以按照启动时的快速入门指南进行注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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