How To Access App Setting Azure Functions

How To Access App Setting Azure Functions

In this Azure tutorial, we will discuss How To Access App Setting Azure Functions. Along with this, we will also discuss a few other topics like How to Access the App settings in Azure Functions Runtime Version 3, Azure Functions v3 configuration, How to Access the App settings in Azure Functions Runtime Version 1, How to access application settings in Azure Functions (ASP.NET Core), Azure Function App Settings JSON. Along with this, we will also discuss Manage Application Settings for Azure Functions within Visual Studio, and Azure Function Environment Variable, Azure Function App Permissions, How Do I Get Azurewebjobsdashboard Connection String.

How To Access App Setting Azure Functions? There are different ways to access the App settings using C# code based on different Azure Function Versions

  • For the Azure Function Version V3, we can use the using statement in the code i.e using Microsoft.Extensions.Configuration. Along with this, we need to make a few changes in the local.settings.json file.

We will discuss How To Access App Setting Azure Functions from the C# code in detail below.

How To Access App Setting Azure Functions

Well, 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 Access the App settings in 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 a few important things we need to remember like we must add the using statement i.e using Microsoft.Extensions.Configuration. Along with this while implementing inside the Azure Function code we need to add the ExecutionContext context as an injection parameter to the Azure Function.

Azure Functions v3 configuration

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 few lines of code that are must to access the App settings in Azure Functions Runtime Version 3 as highlighted below

How to Access the App settings in Azure Functions Runtime Version 3

We are reading the app settings key value from the local.settings.json file, So we need to do few changes in that file as well. 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 Access the App settings in Azure Functions Runtime Version 1

If we will 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"];

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

How to access application settings in Azure Functions (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 way. The syntax will be like this

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

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

You just 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, this is because the local.settings.json file reference we will not get once deployed to Azure.

Azure Function App Settings JSON

One important thing to note down here is Appsettings.json is already deprecated from long back. That was the old name for the local.settings.json file. The new file is local.settings.json file now. All the configuration for the Azure Functions

Manage Application Settings for Azure Functions within Visual Studio

When you are working with the Azure Function, If you want to Add, Edit, or Delete any of the application settings. We normally use to login to the Azure Portal and used to navigate to our Azure function app and then from the Application settings section, we use to perform all the operations as shown below.

Manage Application Settings for Azure Functions within Visual Studio

But there is one more way that you can Manage the Application settings from the Visual Studio itself. If you can see the below screenshot, we can click on the Edit Azure App Service settings link as highlighted below.

Manage Application Settings for Azure Functions using Visual Studio

If you will click on the + Add Setting, you can able to see the below window to add the App settings.

Manage Application Settings for Azure Functions in Visual Studio

How do I access application settings in Azure

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

Step-1: Install the required Nuget packages

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

Step-2: Use the below using statement.

using Microsoft.Extensions.Configuration;

Step-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)

Step-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();

Step-5: The next step is to refer the AppSettings Keys

var appSettingsVal = newconfiguration["appSettingKey"];

This is how to access app setting Azure Functions.

How Do I Add A Connection String In Azure

One of the best way is, You can add a connection string for your Azure Function App. To do that you can follow the Below steps

Log in to the Azure Portal.

Navigate to your Azure Function App

On the function App page, you can click on the Configuration from the left side menu —> Click on the Application Settings tab from the top and Now click on the + New connection string button as highlighted below to create the new connection string.

How Do I Add A Connection String In Azure

Now you have created the Connection string for your Azure Function, To read that from your code you can use the code format like below

var myconn = System.Configuration.ConfigurationManager
                 .ConnectionStrings["MyConnection"].ConnectionString;

In the case, if you are trying to use other than the .Net language, then you can use the Application settings and By the help of the Environment variable, you can easily able to access the App settings from your code.

How Do I Get Azurewebjobsdashboard Connection String

Azurewebjobsdashboard is a Storage account that is used by the Azure WebJob SDK to store the different logs that are being generated from the WebJobs Dashboard. You can get the Azurewebjobsdashboard from the Azure Portal. Follow the below steps to get the Azurewebjobsdashboard Connection String from the Azure Portal.

Login to the AzurePortal (https://portal.azure.com/)

Navigate to your Storage account, For that, You can search for the Storage Account in the Azure Portal, Once you logged in, click on the search result Storage accounts

How Do I Get Azurewebjobsdashboard Connection String

Now you will see all the list of your Storage Accounts, Click on the desired storage account.

On the Storage account page, click on the Access keys from the left navigation under Settings and then click on the Copy button on the Connection string to copy the connection string as highlighted below.

How Do I access Azurewebjobsdashboard Connection String

Azure Function Environment Variable

Environment Variable helps you to store any custom settings for your Azure Function. This is the main purpose of the Environment Variable.

You can configure or manage these custom settings in the Application Settings of the Azure Function App. To reach out to this option, You can log in to the Azure Portal and then navigate to your Azure Function App and then click on the Configuration from the left side menu on your Azure Function App page. Now you can find the option Application settings.

Azure Function Environment Variable

We can use the GetEnvironmentVariable() to access the value of the Environment Variable.

If we will see an example

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

var NewSetting =  Environment.GetEnvironmentVariable("MyNewSetting", EnvironmentVariableTarget.Process);
    return Task.FromResult(req.CreateResponse(HttpStatusCode.OK, new { setting=  NewSetting }));

Now in your local.settings.json you should mention “MY_PRODUCT_NAME” and the value for this key. GetEnvironmentVariable(“MY_PRODUCT_NAME”) retrieves the value for this key that is “Your Product name” in this case.

{
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "MY_PRODUCT_NAME": "Your Product Name"

  }
}

Azure Function App Permissions

Azure Function App permissions effect at the function app level. You can utilize the Role-based permission for your Azure Function App. There are different Roles that are available in the case of the Azure Function App. These areas below

  • Contributor 
  • Owner
  • Reader

Contributor 

The Contributor plays a vital role in the case of Azure Function. Most of the Azure Function tasks are managed by the Contributor.

Owner

Only the Owner can able to delete the Azure Function app when there is a need to delete the Azure Function App. All other tasks can be performed by the Contributor in the case of Azure Function.

Reader

As the name suggest, the Reader can able to view the Azure Function App.

You may also like following the below Articles

Wrapping Up

Well, in this article, we discussed How To Access App Setting Azure Functions, How to Access the App settings in Azure Functions Runtime Version 3, Azure Functions v3 configuration and we also discussed How to Access the App settings in Azure Functions Runtime Version 1, How to access application settings in Azure Functions (ASP.NET Core), Azure Function App Settings JSON and finally, we discussed Azure Function App Settings JSON, Manage Application Settings for Azure Functions within Visual Studio and Azure Function Environment Variable, Azure Function App Permissions, How Do I Get Azurewebjobsdashboard Connection String. Hope you have enjoyed this article !!!.

Leave a Comment