Azure/azure-functions-dotnet-worker

Unable to load appsettings.json files in isolated functions app

jpoalaska opened this issue · 3 comments

Description

I'm running an isolated worker function on .net 9 using the asp.net core integration configuration and I'm trying to get appsettings files to load. When I call builder.Configuration.AddJsonFile(filepath, false) I get an exception:

The configuration file 'appsettings.test.json' was not found and is not optional. The expected physical path was '/tmp/functions\\standby\\wwwroot/appsettings.test.json'.\n at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)\n at Microsoft.Extensions.Configuration.ConfigurationManager.AddSource(IConfigurationSource source)\n at Microsoft.Extensions.Configuration.ConfigurationManager.Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(IConfigurationSource source)\n at Program.<Main>$(String[] args)

Did something change from .net 8 to 9 or maybe in the new Microsoft.Azure.Functions.Worker 2.0 package? I don't think I understand what's happening with this /tmp/functions\standby folder.

My workaround for now until I understand what I'm doing wrong

var binDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
builder
    .AddJsonFile(Path.Combine(binDirectory, "appsettings.json")) 
    .AddJsonFile(Path.Combine(binDirectory, $"appsettings.{environment}.json"), false);

You can try the following.

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

You can try the following.

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

Thanks for the suggestion. I tried the above and noticed the following directory locations. The app is hosted on Linux FYI.

Directory.GetCurrentDirectory() => /tmp/functions\standby\wwwroot
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location => /home/site/wwwroot

There are no appsettings.json files found in Directory.GetCurrentDirectory() so I've stayed with my current workaround for now.