0

I have app.config file in the Azure function that I want to read appsettings at runtime. I updated csproj file to add app.config file to be dropped at 'bin\Debug\netcoreapp3.1' as well as bin\Debug\netcoreapp3.1\bin

but I am getting null value when reading appsettings value. Also, I tried with version 5.0 and 6.0 of the System.Configuration.ConfigurationManager but no luck.

I need help resolving this problem. I want to access app.config file run time. not sure what i am missing

Does Azure function supports accessing app.config file ?. i don't want to use application settings in the function app or local.settings as i have too many strings

Umapathy
  • 23
  • 4

1 Answers1

0

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.

SaiSakethGuduru
  • 2,218
  • 1
  • 5
  • 15