50

I am writing an Azure function in VS 2017. I need to set up a few custom configuration parameters. I added them in local.settings.json under Values.

{
   "IsEncrypted":false,
   "Values" : {
      "CustomUrl" : "www.google.com",
       "Keys": { 
           "Value1":"1",
           "Value2" :"2"
       }
   }
}

Now, ConfigurationManager.AppSettings["CustomUrl"] returns null.

I'm using:

  • .NET Framework 4.7
  • Microsoft.NET.Sdk.Functions 1.0.5
  • System.Configuration.ConfigurationManager 4.4.0
  • Azure.Functions.Cli 1.0.4

Am I missing something?

9
  • This should work. Can you step into debugger and see what's in ConfigurationManager.AppSettings? Commented Oct 12, 2017 at 17:17
  • 2
    make sure local.settings.json is marked to always be copied to the build output. Commented Oct 12, 2017 at 17:57
  • @Mikhail - ConfigurationManager.AppSettings shows an object with Count = 0, KeyCollection = 0 etc.. Commented Oct 12, 2017 at 18:04
  • 1
    @ahmelsayed it's already set "Copy Always" and build action is None. Commented Oct 12, 2017 at 18:04
  • 1
    It's very odd. I don't know what could be happening, but here is what should happen. When you run from VS, it's supposed to compile and copy all your files to the output path, something like bin\Debug\net461. There you should find a folder per function, and your host.json and local.settings.json. After that, VS launches Azure.Functions.Cli.exe from the path above, with that folder as the current working directory, and host start as args. That should copy all your settings from local.settings.json to Azure.Functions.Cli.exe.config in that folder. Commented Oct 12, 2017 at 18:31

7 Answers 7

89

Environment.GetEnvironmentVariable("key")

I was able to read values from local.settings.json using the above line of code.

Sign up to request clarification or add additional context in comments.

3 Comments

Microsoft recommends this method because it works both locally with local.settings.json and in Azure. learn.microsoft.com/en-us/azure/azure-functions/…
How do we access keys in ConnectionStrings object using Environment ?
this is the correct answer when we use function app
23

Firstly, I create a sample and do a test with your local.settings.json data, as Mikhail and ahmelsayed said, it works fine.

Besides, as far as I know, Values collection is expected to be a Dictionary, if it contains any non-string values, it can cause Azure function can not read values from local.settings.json.

My Test:

ConfigurationManager.AppSettings["CustomUrl"] returns null with the following local.settings.json.

{
  "IsEncrypted": false,
  "Values": {
    "CustomUrl": "www.google.com",
    "testkey": {
      "name": "kname1",
      "value": "kval1"
    }
  }
}

enter image description here

4 Comments

Thanks @Fred .. I was having another object inside Values. My bad.
Look at the post by @Chris Koester regarding the new recommended way: learn.microsoft.com/en-us/azure/azure-functions/…
To be more precise, Azure Functions simply don't support nested settings, i.e. the local.settings.json file does not work the same as appsettings.json in .NET Core: github.com/Azure/azure-functions-host/issues/…
@Venky That's true but if you want to have nested key values you can do sth like that: nested keys
9

Using .Net 6 (and probably some earlier versions) it is possible to inject IConfiguration into the constructor of the function.

public Function1(IConfiguration configuration)
{
    string setting = _configuration.GetValue<string>("MySetting");
}

MySetting must be in the Values section of local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "MySetting": "value"
  }
}

It works with Application settings in Azure Function App as well.

2 Comments

Azure Functions, as far as I know are static. So can you please elaborate how you are doing DI?
Functions can be instance method. See this link.
6

Azure function copies the binaries to the bin folder and runs using the azure function cli, so it searches for the local.settings.json, so make sure you have set the "Copy to Output Directory" to "Copy Always"

enter image description here

Comments

6

If you are using TimeTrigger based Azure function than you can access your key (created in local.settings.json) from Azure Function as below.

[FunctionName("BackupTableStorageFunction")]
public static void Run([TimerTrigger("%BackUpTableStorageTriggerTime%")]TimerInfo myTimer, TraceWriter log, CancellationToken cancellationToken)

Comments

1

Hey you mmight be able to read the properties while debugging, but once you go and try to deploy that in azure, those properties are not going to work anymore. Azure functions does not allow nested properties, you must use all of them inline in the "Values" option or in "ConnectionStrings". Look at this documentation as reference: https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings

Comments

1

var value = Environment.GetEnvironmentVariable("key", EnvironmentVariableTarget.Process); should be the more appropriate answer, though EnvironmentVariableTarget.Process is the default value but it's more meaningful here.

Look at its EnvironmentVariableTarget declaration.

//
// Summary:
//     Specifies the location where an environment variable is stored or retrieved in
//     a set or get operation.
public enum EnvironmentVariableTarget
{
    //
    // Summary:
    //     The environment variable is stored or retrieved from the environment block associated
    //     with the current process.
    Process = 0,
    //
    // Summary:
    //     The environment variable is stored or retrieved from the HKEY_CURRENT_USER\Environment
    //     key in the Windows operating system registry. This value should be used on .NET
    //     implementations running on Windows systems only.
    User = 1,
    //
    // Summary:
    //     The environment variable is stored or retrieved from the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session
    //     Manager\Environment key in the Windows operating system registry. This value
    //     should be used on .NET implementations running on Windows systems only.
    Machine = 2
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.