Jack
Jack

Reputation: 307

Azure Functions Environment.GetEnvironmentVariable returns null

I have an Azure Functions app that I'm testing locally but it doesn't appear to be picking up my local.settings.json configuration. I had to manually create the file since I've cloned this solution from my github repo (local.settings.json is set to ignore by default). I'm wondering if that's the issue and there's some way I'm not linking it to the project?

Project Settings

local.settings.json

Calling code

Upvotes: 4

Views: 15198

Answers (3)

Stefan
Stefan

Reputation: 1225

Check if the file build property Copy to Output Directory of the local.settings.json file is set to: Copy if newer. If not, change it.

enter image description here

The local.settings.json file needs to be in the binary folder at runtime. Else, these values ​​are not entered into the environment variables.

If you messed around with the file (like removed it from your project or copied it from another source and included it into your project) this file build property is set by default to Do not copy which causes the issue.

Upvotes: 1

Aurelius
Aurelius

Reputation: 112

make sure your local.settings.json is in the same place as the host.json at the project root and do a rebuild of your project after adding it also I get my azure function environment variables like so:

Environment.GetEnvironmentVariable("Issuer", EnvironmentVariableTarget.Process);

Upvotes: 1

suziki
suziki

Reputation: 14088

Update:

Since it is not the key. try to add json file to env on hand. Add a Startup class to your functionapp.

public class Startup : IWebJobsStartup
{
    public void Configure(IWebJobsBuilder builder)
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
    }
}

Original Answer:

My guess is because this local.settings.json was created by yourself, so this file was not placed under the debug folder after compilation. Therefore, these values ​​are not entered into the environment variables.

Based on this, I can give a solution, copy your local.settings.json file directly to the below folder, and then the problem should be solved.

enter image description here

(There should be a local.settings.json file here, otherwise the environment variables cannot be entered.)

Hope my inference is correct and hope this answer helps. :)

Upvotes: 1

Related Questions