Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add configuration values in AppSettings.json in Azure functions. Is there any structure for it?

What is the standard structure to add keys to appsettings.json? Also, how to read those values in our run.csx? Normally in app.config, we had ConfigurationManager.GetSettings["SettingName"]; Is there any similar implementation in Azure Function?

like image 933
Harshit Agarwal Avatar asked Feb 20 '17 07:02

Harshit Agarwal


People also ask

How do I add a config file in Azure?

To begin, go to the Azure portal and sign in to your Azure account. In the search bar at the top of the portal, enter the name of your function app and select it from the list. Under Settings in the left pane, select Configuration.

Does APP service configuration override Appsettings json?

For ASP.NET and ASP.NET Core developers, setting app settings in App Service are like setting them in <appSettings> in Web. config or appsettings. json, but the values in App Service override the ones in Web.

How do I get Azure app configuration connection string?

You can find the connection string under Access Keys in the Azure portal.


3 Answers

In Azure Functions 2.x, you need to use the .Net core configuration management style, contained in package Microsoft.Extensions.Configuration. This allows you to create a local settings.json file on your dev computer for local configuration in the Values and ConnectionString portion of the json file. The local json settings file isn't published to Azure, and instead, Azure will obtain settings from the Application Settings associated with the Function.

In your function code, accept a parameter of type Microsoft.Azure.WebJobs.ExecutionContext context, where you can then build an IConfigurationRoot provider:

[FunctionName("MyFunction")]
public static async Task Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer,
    TraceWriter log, Microsoft.Azure.WebJobs.ExecutionContext context, 
    CancellationToken ctx)
{
   var config = new ConfigurationBuilder()
        .SetBasePath(context.FunctionAppDirectory)
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

    // This abstracts away the .json and app settings duality
    var myValue = config["MyKey"];

    var myConnString = config.GetConnectionString("connString");
    ... etc

The AddJsonFile allows you to add a local development config file e.g. local.settings.json containing local dev values (not published)

{
  "IsEncrypted": false,
  "Values": {
    "MyKey": "MyValue",
     ...
   },
   "ConnectionStrings": {
      "connString": "...."
}

Although seemingly using ConnectionStrings for anything other than EF seems to be discouraged

And once deployed to Azure, you can change the values of the settings on the function Application Settings blade:

Application Config

like image 200
StuartLC Avatar answered Oct 16 '22 21:10

StuartLC


As stated here

These settings can also be read in your code as environment variables. In C#, use System.Environment.GetEnvironmentVariable or ConfigurationManager.AppSettings. In JavaScript, use process.env. Settings specified as a system environment variable take precedence over values in the local.settings.json file.

like image 21
Kevin Smith Avatar answered Oct 16 '22 22:10

Kevin Smith


You don't have to use System.Environment.GetEnvironmentVariable() to access your app settings.

ConfigurationManager is available to Azure Functions in run.csx like so:

System.Configuration.ConfigurationManager.AppSettings["SettingName"]
like image 3
urig Avatar answered Oct 16 '22 20:10

urig