1
votes

I need to set a Azure Functions app's base directory to the azurewebjobsscriptroot like below, but got exception

var config = new ConfigurationBuilder()
    .SetBasePath("%HOME%\site\wwwroot")   //error
    .AddJsonFile("Settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

System.ArgumentException : The path must be absolute. Parameter name: root at Microsoft.Extensions.FileProviders.PhysicalFileProvider..ctor(String root,ExclusionFilters filters) at Microsoft.Extensions.Configuration.FileConfigurationExtensions.SetBasePath(IConfigurationBuilder builder,String basePath)

Azure function 2.x

VS 2017

ExecutionContext is null to non-function methods via IoC, alternative to ExecutionContext.FunctionAppDirectory

https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings#azurewebjobsscriptroot

1

1 Answers

1
votes

You probably want to expand the path first before using it. That way the environment variable(s) embedded in the string can be replaced with the equivalent value of the variable. Resulting in a valid base path for the configuration.

var AzureWebJobsScriptRoot = "%HOME%\site\wwwroot";

var expandedRootPath = Environment.ExpandEnvironmentVariables(AzureWebJobsScriptRoot);

var config = new ConfigurationBuilder()
    .SetBasePath(expandedRootPath)
    .AddJsonFile("Settings.json", optional: true, reloadOnChange: true)
    .AddEnvironmentVariables()
    .Build();

Reference Environment.ExpandEnvironmentVariables(String) Method