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

7

21

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?

Exam answered 20/2, 2017 at 7:3 Comment(0)
W
15

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.

Warrantable answered 31/8, 2017 at 14:35 Comment(0)
S
28

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

Swashbuckler answered 21/12, 2018 at 9:13 Comment(6)
The Azure application settings correspond to the Values settings and Connection Strings represents ConnectionStrings section of the json file.Swashbuckler
When deployed instead of reading from function configuration settings, is there a way to read from an appsettings.json file deployed with the contents?Unrig
This should be the accepted anwer. I found this very helpful. Thank you.Shyamal
Why should one add this code if one can just add the local file to the sln for local development or keys to the azure func configurations and use System.Environment.GetEnvironmentVariable("keyName") ? Getting an env var sounds much easier and also saves you the trouble of passing this config var everywhereElanorelapid
@Elanorelapid - I would like to see a response message in postman whenever a function is successfully triggered. How can we test azure functions in postman?Mushroom
You can invoke their url in postmanElanorelapid
W
15

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.

Warrantable answered 31/8, 2017 at 14:35 Comment(0)
D
3

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"]
Disannul answered 18/4, 2017 at 14:59 Comment(0)
C
2

To load the environment or appsettings value you need to use the

System.Environment.GetEnvironmentVariable property

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    log.Info(GetEnvironmentVariable("AzureWebJobsStorage"));
    log.Info(GetEnvironmentVariable("WEBSITE_SITE_NAME"));
}

public static string GetEnvironmentVariable(string name)
{
    return name + ": " + 
        System.Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.Process);
}

Manage app settings variables - https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings

Concertino answered 20/2, 2017 at 7:50 Comment(4)
what about the other part of his question?Tarsia
which part ? in general variables are stored in a key value fashion. there is no change there.Concertino
What is the standard structure to add keys to appsettings.json?Tarsia
learn.microsoft.com/en-us/azure/azure-functions/…Concertino
L
1

Azure functions support only limited part of app.config. It allows to save app settings and connections in local.settings.json when running function from VS. It don't support WCF endpoint settings under system.serviceModel in this json file. I had a dll library reference in Azure Function and that was internally calling WCF apis.

Strange thing I found is, when I run the Azure function, it converts back the json to xml config at the cli path (%localappdata%\AzureFunctionsTools\Releases\1.6.0\cli\func.exe.config). I added my xml configuration hierarchy (system.serviceModel) to this config file and it worked fine, picking my WCF endpoints to run the services. Though have struggled in using log4net configuration but am good to run the APIs. Azure should have supported the xml config files directly. Hope this helps.

Liebermann answered 20/12, 2018 at 19:46 Comment(0)
S
1

Here's how you can set it up:

Step 1

Add your json at the root of your repo, example app.settings.json

Step 2

Add the Diretory.Build.targets (.targets is the extension here) file as follows

<Project>
  <PropertyGroup>
    <_IsFunctionsSdkBuild Condition="$(_FunctionsTaskFramework) != ''">true</_IsFunctionsSdkBuild>
    <_FunctionsExtensionsDir>$(TargetDir)</_FunctionsExtensionsDir>
    <_FunctionsExtensionsDir Condition="$(_IsFunctionsSdkBuild) == 'true'">$(_FunctionsExtensionsDir)bin</_FunctionsExtensionsDir>
  </PropertyGroup>

  <Target Name="CopyExtensionsJson" AfterTargets="_GenerateFunctionsAndCopyContentFiles">
    <Message Importance="High" Text="Overwritting extensions.json file with one from build." />

    <Copy Condition="$(_IsFunctionsSdkBuild) == 'true' AND Exists('$(_FunctionsExtensionsDir)\extensions.json')"
          SourceFiles="$(_FunctionsExtensionsDir)\extensions.json"
          DestinationFiles="$(PublishDir)bin\extensions.json"
          OverwriteReadOnlyFiles="true"
          ContinueOnError="true"/>
  </Target>

  <Target Name="CopyVaultJson" AfterTargets="_GenerateFunctionsAndCopyContentFiles">
    <Message Importance="High" Text="Overwritting app.settings.json file with one from build." />

    <Copy Condition="$(_IsFunctionsSdkBuild) == 'true' AND Exists('$(_FunctionsExtensionsDir)\app.settings.json')"
          SourceFiles="$(_FunctionsExtensionsDir)\app.settings.json"
          DestinationFiles="$(PublishDir)bin\app.settings.json"
          OverwriteReadOnlyFiles="true"
          ContinueOnError="true"/>
  </Target>
</Project>

This will explicitly tell the compiler to include the app.settings.json file when dotnet build is ran and will include said file in a the /bin, allowing your dll's to access it.

Happy coding.

Semela answered 20/5, 2019 at 8:3 Comment(0)
I
0

Appsettings are not managed by the function itself, but by its Function App. So, if you use the cli, is something along...

az functionapp appsettings set .....

That's how I do it in my CI/CD pipeline. After that, you can use them in your functions. Remember that a function MUST live within a Function App, so it makes total sense to place all those values there so that you have them available in every function.

Islamize answered 6/2, 2019 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.