如何在函数启动类中访问ExecutionContext.FunctionAppDirectory,以便正确设置我的配置。请参考以下启动代码:
[assembly: WebJobsStartup(typeof(FuncStartup))]
namespace Function.Test
{
public class FuncStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(“”/* How to get the Context here. I cann’t DI it
as it requires default constructor*/)
.AddJsonFile(“local.settings.json”, true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}
}
}发布于 2019-04-11 01:18:58
你没有ExecutionContext,因为你的Azure函数还没有处理实际的函数调用。但是您也不需要它- local.settings.json会自动解析成环境变量。
如果你真的需要这个目录,你可以在Azure中使用%HOME%/site/wwwroot,在本地运行时使用AzureWebJobsScriptRoot。这相当于FunctionAppDirectory。
This也是关于这个话题的一个很好的讨论。
public void Configure(IWebJobsBuilder builder)
{
var local_root = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
var azure_root = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
var actual_root = local_root ?? azure_root;
var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.SetBasePath(actual_root)
.AddJsonFile("SomeOther.json")
.AddEnvironmentVariables()
.Build();
var appInsightsSetting = config.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY");
string val = appInsightsSetting.Value;
var helloSetting = config.GetSection("hello");
string val = helloSetting.Value;
//...
}示例local.settings.json:
{
"IsEncrypted": false,
"Values": {
"APPINSIGHTS_INSTRUMENTATIONKEY": "123456..."
}
}示例SomeOther.json
{
"hello": "world"
}发布于 2019-08-28 02:19:46
使用下面的代码,它对我很有效。
var executioncontextoptions = builder.Services.BuildServiceProvider()
.GetService<IOptions<ExecutionContextOptions>>().Value;
var currentDirectory = executioncontextoptions.AppDirectory;
configuration = configurationBuilder.SetBasePath(currentDirectory)
.AddJsonFile(ConfigFile, optional: false, reloadOnChange: true)
.Build();发布于 2021-10-19 16:43:56
当文档说明函数应用程序中的默认值为%HOME%\site\wwwroot时,这意味着如果您不指定此环境变量,函数主机将使用%HOME%\site\wwwroot。
public void Configure(IWebJobsBuilder builder)
{
var localRoot = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
var azureRoot = $@"{Environment.GetEnvironmentVariable("HOME")}\site\wwwroot";
var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.SetBasePath(localRoot ?? azureRoot )
.AddJsonFile("appsettings.json", true)
.AddJsonFile("local.settings.json", true)
.AddEnvironmentVariables()
.Build();
}https://stackoverflow.com/questions/55616798
复制相似问题