Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load appsettings.json files in isolated functions app #2908

Open
jpoalaska opened this issue Dec 20, 2024 · 3 comments
Open

Unable to load appsettings.json files in isolated functions app #2908

jpoalaska opened this issue Dec 20, 2024 · 3 comments

Comments

@jpoalaska
Copy link

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.

@jpoalaska
Copy link
Author

jpoalaska commented Dec 20, 2024

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

@traviie
Copy link

traviie commented Dec 27, 2024

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

@jpoalaska
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants