In my case the context.HostingEnvironment.ContentRootPath
did not contain my local appsettings.json
file.
├───azure-functions-host
│ └───appsettings.json //this version was truncated?
├───home
│ └───site
│ └──wwwroot
│ ├───appsettings.json //this version was the correct published version
- Using the
context.HostingEnvironment.ContentRootPath
as mentioned here https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources pointed to the path\azure-functions-host
in my local docker instance.
This folder curiously did contain appsettings.json
file but it was truncated and did not contain the correct information. This threw me off for a while as I assumed it would have.
- The publish folder at
/home/site/wwwroot/
did contain the correctappsettings.json
file. - I found that there was an environment variable
AzureWebJobsScriptRoot
which pointed to the correct location. So my solution was to do the following in my startup code
var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureAppConfiguration((context, builder) =>
{
string contentRootPath = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot") ?? context.HostingEnvironment.ContentRootPath;
builder
.SetBasePath(contentRootPath)
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
})
.Build();