gpt4 book ai didi

azure - Azure Function IWebJobsStartup 实现中的 ExecutionContext

转载 作者:行者123 更新时间:2023-12-02 05:19:33 26 4
gpt4 key购买 nike

如何访问 Functions Startup 类中的 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();

}
}
}

最佳答案

您没有 ExecutionContext,因为您的 Azure Function 尚未处理实际的函数调用。但您也不需要它 - 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"
}

关于azure - Azure Function IWebJobsStartup 实现中的 ExecutionContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55616798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com