To access app settings 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.
Below is the syntax for for configurationbuilder
var value = Environment.GetEnvironmentVariable("Put the key value");
For local development you can add custom settings to local.settings.json file but this settings will be useful only for local development if you deploy to Azure you won't get that reference.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"custom key": "custom value"
}
}
In this case your function may look like this
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");
Check ConfigurationBuilder for further details.