简体   繁体   中英

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, ie 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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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