Azure Function Get App Settings C#

How To Access App Setting Azure Functions

This Azure tutorial will discuss Azure Functions app settings and how to read app settings in Azure Functions c#.

Azure Function Get App Settings C#

Let’s Discuss How to access Azure Function Application Settings from C#. As part of the discussion, we will discuss How to access application settings in different runtime versions of Azure Functions.

How to read App Settings Azure Functions Runtime Version 3

In the Azure Functions Runtime Versions 3, Accessing the Application Settings is a bit easier compared to the other Azure Functions Runtime Versions like Azure Function Version V2 and Azure Function Version V1.

Here are a few important things we must remember, like adding the using statement, i.e., using Microsoft.Extensions.Configuration. Additionally, while implementing inside the Azure Function code, we need to add the ExecutionContext context as an injection parameter to the Azure Function.

If We will consider an example here, we can see My Azure Function Code as below

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Extensions.Configuration;

namespace FunctionApp5
{
    public static class MyProductHTTPFunction
    {
        [FunctionName("MyProductHTTPFunction")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("MyProductHTTPFunction function processed a request.");

            var configurationBuilder = new ConfigurationBuilder()
                .SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) 
                .AddEnvironmentVariables() 
                .Build();

            string myappsettingsValue = configurationBuilder["appsettingkey"];

       

You can concentrate on a few lines of code that are a must to access the App settings in Azure Functions Runtime Version 3, as highlighted below

azure function app read configuration settings c#

We are reading the app settings key value from the local.settings.json file, So we also need to make a few changes in that file. So your local.settings.json file should look like below.

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "appsettingkey": "Your App Settings key value"

  }
}

How to get the App Settings in Azure Functions Runtime Version 1

If we go back to the Older version of Azure Functions i.e. Runtime version 1 then we can see there, we were using the ConfigurationManager.

For example, the implementation was like below

var yourclientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];
var yourclientSecret = System.Configuration.ConfigurationManager.AppSettings["ClientSecret"];
var youraad = System.Configuration.ConfigurationManager.AppSettings["AADDomain"];

We are not using the ConfigurationManager; instead, we are using the ConfigurationBuilder from the Azure Function runtime version V2 onwards.

How to access Azure Function application settings (ASP.NET Core)

Earlier, in the case of Azure Function Runtime version V1, We were using the ConfigurationManager. But then, on the latest versions of Azure Function, we are using the ConfigurationBuilder instead.

We can also use the System.Environment.GetEnvironmentVariable to read the application settings or app settings.

his is one of the recommended ways. The syntax will be like this

var value = Environment.GetEnvironmentVariable("Put the key value");

You can add some custom settings in your local.settings.json file for local development. One important point to note here is that the settings you add in the local.settings.json file will be useful for your local development. Once you deploy to Azure, you will not get that reference. So, this is one you can keep in your mind.

You need to add one more key and value in the Local.settings.json file

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "custom key": "custom value"

  }
}

The Azure function will look something like below

using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Extensions.Configuration;

namespace FunctionApp5
{
    public static class MyNewHTTPFunction
    {
        [FunctionName("MyNewHTTPFunction
")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, ExecutionContext context)
        {
            log.LogInformation("MyProductHTTPFunction function processed a request.");

            var newconfiguration = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory)
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();
var appSettingsVal = newconfiguration["appSettingKey"];
var myconnection = newconfiguration.GetConnectionString("YourSqlConnectionString");

in the above code, we are adding the optional: true, because the local.settings.json file reference we will not get once deployed to Azure.

Check out: Azure Function appsettings.json

FAQs

How do I access application settings in Azure

Above, we have already discussed this with examples, I will just explain with specific to the point here.

1. Install the required Nuget packages

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Configuration.FileExtensions

2. Use the below-using statement.

using Microsoft.Extensions.Configuration;

3. Add the ExecutionContext as a parameter to your Azure Function

[FunctionName("MyNewHTTPFunction
")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log, ExecutionContext context)

4. Now you need to get the IConfiguration Root using the below lines of Code.

var myConfig = new ConfigurationBuilder()
    .SetBasePath(context.FunctionAppDirectory)
    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

5. The next step is to refer to the AppSettings Keys

var appSettingsVal = newconfiguration["appSettingKey"];

You may also like following the below Articles

Wrapping Up

In this article, we discussed Azure Functions app settings and how to read app settings in Azure Functions c#. I hope you have enjoyed this article !!!.