47

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90
  • This should work. Can you step into debugger and see what's in `ConfigurationManager.AppSettings`? – Mikhail Shilkov Oct 12 '17 at 17:17
  • 1
    make sure `local.settings.json` is marked to always be copied to the build output. – ahmelsayed Oct 12 '17 at 17:57
  • @Mikhail - `ConfigurationManager.AppSettings` shows an object with Count = 0, KeyCollection = 0 etc.. – Venkata Dorisala Oct 12 '17 at 18:04
  • 1
    @ahmelsayed it's already set "Copy Always" and build action is None. – Venkata Dorisala Oct 12 '17 at 18:04
  • under `C:\Users\\AppData\Local\Azure.Functions.Cli`, what versions do you have? – ahmelsayed Oct 12 '17 at 18:10
  • `Azure.Functions.Cli : 1.0.4` – Venkata Dorisala Oct 12 '17 at 18:12
  • 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. – ahmelsayed Oct 12 '17 at 18:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156586/discussion-between-ahmelsayed-and-venky). – ahmelsayed Oct 12 '17 at 18:31
  • Nested objects in config are supported in functions v2 (Net Core) but don't seem to work in v1 (Net Framework) – Mariusz Feb 21 '19 at 09:36

7 Answers7

83

Environment.GetEnvironmentVariable("key")

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

jayasurya_j
  • 1,519
  • 1
  • 15
  • 22
  • 12
    Microsoft recommends this method because it works both locally with local.settings.json and in Azure. https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables – Chris Koester Sep 11 '18 at 18:06
  • 2
    How do we access keys in ConnectionStrings object using Environment ? – Venkata Dorisala Jan 03 '19 at 09:55
  • 2
    this is the correct answer when we use function app – Neo Oct 14 '20 at 10:25
22

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

Fei Han
  • 26,415
  • 1
  • 30
  • 41
  • Thanks @Fred .. I was having another object inside `Values`. My bad. – Venkata Dorisala Oct 13 '17 at 11:04
  • 1
    Look at the post by @Chris Koester regarding the *new* recommended way: https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library#environment-variables – Jason Shave Nov 08 '18 at 04:13
  • 4
    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: https://github.com/Azure/azure-functions-host/issues/4737#issuecomment-325045802 – Ian Kemp Aug 01 '19 at 11:28
  • @Venky That's true but if you want to have nested key values you can do sth like that: [nested keys](https://github.com/Azure/azure-functions-core-tools/issues/1473#issuecomment-393303529) – krypru Nov 06 '19 at 14:54
6

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.

AlbertK
  • 11,841
  • 5
  • 40
  • 36
  • Azure Functions, as far as I know are static. So can you please elaborate how you are doing DI? – VivekDev Jan 28 '22 at 03:31
  • 3
    Functions can be instance method. See [this](https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#use-injected-dependencies) link. – AlbertK Jan 28 '22 at 08:29
5

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

executable
  • 3,365
  • 6
  • 24
  • 52
5

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)
Ashish-BeJovial
  • 1,829
  • 3
  • 38
  • 62
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

Herberth Gomez
  • 187
  • 1
  • 2
  • 19
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
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197