DalhousieDuck
DalhousieDuck

Reputation: 339

Why does my azure function sometimes fail to read a static configuration variable?

I have an azure function that needs to make an http call, and it retrieves the uri from a configuration variable that I've ensured exists in the function configuration section, in the Azure portal.

When I run my function in Azure, I've noticed that roughly 80% of the time, my function fails to read the value from the function configuration, i.e. the Environment.GetEnvironmentVariable("endpoint_value_from_configuration") method call returns null.

However if I repeatedly test this function (without changing any code or configuration whatsoever) it does sometimes succeed to read the variable. What's going on here? How can I change this function so that this problem does not occur?

[FunctionName("MessageHandler")]
public async Task Run([ServiceBusTrigger("%Topic%", "subname", Connection = "ServiceBusConnection")] string mySbMsg,
    ILogger log)
{
    var obj = JsonConvert.DeserializeObject<BusinessObject>(mySbMsg);
    var endpoint = Environment.GetEnvironmentVariable("endpoint_value_from_configuration");

Upvotes: 0

Views: 318

Answers (1)

Harshita Singh
Harshita Singh

Reputation: 4870

Check the response here by Pitchmatt - Reading settings from a Azure Function

If you are using V2, you need to use below code:

var config = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory)
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

And then, use config to fetch settings:

var endpoint = config["endpoint_value_from_configuration"]

When debugging locally, it gets the settings from local.settings.json under the "Values" keyword. When running in Azure, it gets the settings from the Application settings tab.

Upvotes: 1

Related Questions