re函数IWebJobsStartup实现中的Execution

re函数IWebJobsStartup实现中的Execution

本文介绍了Azure函数IWebJobsStartup实现中的ExecutionContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Functions Startup类中访问ExecutionContext.FunctionAppDirectory,以便我可以正确设置Configuration.请查看以下启动代码:

How to get access to the ExecutionContext.FunctionAppDirectory in Functions Startup class so I can setup my Configuration correct. Please see the following Startup code:

[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会自动解析为环境变量.

You don't have the ExecutionContext since your Azure Function is not yet processing an actual function call. But you don't need it either - the local.settings.json is automatically parsed into the environment variables.

如果确实需要目录,则可以在Azure中使用%HOME%/site/wwwroot ,在本地运行时可以使用 AzureWebJobsScriptRoot .这等效于 FunctionAppDirectory .

If you really need the directory, you can use %HOME%/site/wwwroot in Azure, and AzureWebJobsScriptRoot when running locally. This is the equivalent of FunctionAppDirectory.

也是关于此主题的很好的讨论.

This is also a good discussion about this topic.

    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:

Example local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "APPINSIGHTS_INSTRUMENTATIONKEY": "123456..."
  }
}

示例SomeOther.json

Example SomeOther.json

{
  "hello":  "world"
}

这篇关于Azure函数IWebJobsStartup实现中的ExecutionContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:42