Azure Function IWebJobsStartup 实现中的 ExecutionContext

Posted

技术标签:

【中文标题】Azure Function IWebJobsStartup 实现中的 ExecutionContext【英文标题】:ExecutionContext in Azure Function IWebJobsStartup implementation 【发布时间】:2019-09-01 03:54:38 【问题描述】:

如何访问 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();

        
    
 

【问题讨论】:

【参考方案1】:

您没有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"

【讨论】:

但是,local.settings.json 仅适用于开发环境,如果我想在 Azure 中使用 JSON 文件或单独的文件(如 appsettings.json)进行设置怎么办?当在 docker 中运行函数时,这尤其是一个问题。 我已经更新了我的答案 - 不过要小心将某些设置(如机密)放入 docker 图像中 - 请参阅 medium.com/@mccode/… 所有秘密都通过 Vault 提供给服务,因此它们都不会添加到图像中。让我试试这个解决方案,据我所知 Directory.GetCUrrentDirectory() 不起作用。谢谢 实际上,我的代码在 Azure 中不起作用...github.com/MicrosoftDocs/azure-docs/issues/26761 现在我很好奇 ;-) 将在几分钟后再次更新答案..【参考方案2】:

使用下面的代码,它对我有用。

var executioncontextoptions = builder.Services.BuildServiceProvider()
         .GetService<IOptions<ExecutionContextOptions>>().Value;

var currentDirectory = executioncontextoptions.AppDirectory;

configuration = configurationBuilder.SetBasePath(currentDirectory)
          .AddJsonFile(ConfigFile, optional: false, reloadOnChange: true)    
          .Build();

【讨论】:

【参考方案3】:

当文档说 In a function app, default is %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();            
    

【讨论】:

以上是关于Azure Function IWebJobsStartup 实现中的 ExecutionContext的主要内容,如果未能解决你的问题,请参考以下文章