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

Viewed 430

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

  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

How can I read those config settings correctly ?

3 Answers

You need to use dependency injection and add Azure App Configuration as extra configuration source in order for your app function talk with it.

you can follow the quick start guideline in your startup register them.

Use this code:

Environment.GetEnvironmentVariable("something");
Related