41

I'm new to Azure's function... I've created a new timer function (will be fired every 30 minutes) and it has to perform a query on a URL, then push data on the buffer..

I've done

public static void Run(TimerInfo myTimer, TraceWriter log)
{
 var s = CloudConfigurationManager.GetSetting("url");
 log.Info(s);
}

And in my function settings I've

enter image description here

What am I doing wrong? Thanks

General Grievance
  • 4,555
  • 31
  • 31
  • 45
advapi
  • 3,661
  • 4
  • 38
  • 73

6 Answers6

96

Note that for Azure Functions v2 this is no longer true. The following is from Jon Gallant's blog:

For Azure Functions v2, the ConfigurationManager is not supported and you must use the ASP.NET Core Configuration system:

  1. Include the following using statement:

    using Microsoft.Extensions.Configuration;
    
  2. Include the ExecutionContext as a parameter

    public static void Run(InboundMessage inboundMessage, 
        TraceWriter log,
        out string outboundMessage, 
        ExecutionContext context)
    
  3. Get the IConfiguration Root

    var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();
    
  4. And use it to reference AppSettings keys

    var password = config["password"]
    

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.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
Pitchmatt
  • 1,364
  • 1
  • 9
  • 10
  • 5
    Thanks for this answer, this should be the accepted one per V2 requirements. One thing to note is that in addition to `Microsoft.Extensions.Configuration` you also need `Microsoft.Extensions.Configuration.Json` and `Microsoft.Extensions.Configuration.FileExtensions` for this to work. They are not dependencies of the main package so you need to install them separately with NuGet. – HTBR May 02 '19 at 09:16
  • As said in the starting line, does not holds good for v2. The reply from @KVigor is the good way, that will not alter the existing code. – Prem Feb 17 '22 at 15:37
54

You can use System.Environment.GetEnvironmentVariable like this:

var value = Environment.GetEnvironmentVariable("your_key_here")

This gets settings whenever you're working locally or on Azure.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
DSpirit
  • 2,062
  • 16
  • 22
18

You need to go to Platform Features -> Application settings and add it there.

Application settings

Settings

Add the setting under App settings.


Reading the setting can be done by first adding this at the top:

using System.Configuration;

And then reading a setting with:

string setting = ConfigurationManager.AppSettings["url"];

Where url is your setting key. The setting variable will contain your setting value.

juunas
  • 54,244
  • 13
  • 113
  • 149
3

Recommended way — use environment variable to read settings

string Secret = System.Environment.GetEnvironmentVariable("Secret");

This works perfect if you run from your local PC or from Azure Functions using C#

enter image description here

enter image description here

Shinoy Babu
  • 1,641
  • 2
  • 12
  • 12
3

You don't have to do step 3 anymore. Just do the following in your Startup.cs `

Startup.cs

public IConfiguration Configuration { get; }

public Startup() { }

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

Then use DI in your code file:

` YourCode.cs

public class SomeWorker : ISomeWorker
{
    private readonly IConfiguration _configuration;

    public SomeWorker(IConfiguration configuration)
    {            
        _configuration = configuration;
    }
    
    public void bool ForKicks()
    {
        // Get Value
        var val = _configuration["SomeValueInLocalSettingsJsonFile"];
    }
    
    // Other code here...
}
Kevon
  • 43
  • 7
  • 1
    This is the best answer. The configuration can be created using various places like AppSetting, KeyVault, JSON file etc. This configuration can be passed on to other parts of the program using the DI shown exactly as above. Thanks @KVigor. – Prem Feb 17 '22 at 15:33
3

For those who needs to do it in python :

import os
os.environ['ENVIRONMENT_VARIABLE']
mahmoud mehdi
  • 1,493
  • 1
  • 19
  • 28