【问题标题】:Azure Webjob 3.0 - Unable to read Configuration variable from appsettings.json in Timer FunctionAzure Webjob 3.0 - 无法从计时器函数中的 appsettings.json 读取配置变量
【发布时间】:2019-04-23 09:35:00
【问题描述】:

我正在努力从 Azure Webjob SDK 3.0 项目中的 appsettings.json 文件中读取任何变量。

我正在使用:

  • Microsoft.Azure.WebJobs.Core version="3.0.6"
  • Microsoft.Azure.WebJobs.Extensions version="3.0.2"
  • Microsoft.Extensions.Configuration version="2.2.0"

我尝试了以下方法:

System.Configuration.ConfigurationManager.AppSettings["ApiUrl"]

但这会返回 null 并且无法读取。我已经检查了有关 SO 的文档和其他问题,但目前这就像大海捞针。

程序.cs

static async Task Main(string[] args)
        {
            var builder = new HostBuilder()
                        .ConfigureWebJobs(b =>
                        {
                            b.AddAzureStorageCoreServices()                    
                            .AddAzureStorage()
                            .AddTimers();

                            b.UseHostId(Environment.UserName.ToLowerInvariant());
                        })
                        .ConfigureAppConfiguration((b, c)=>
                        {
                            var env = b.HostingEnvironment;
                            c.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                            c.AddCommandLine(args);
                            c.AddEnvironmentVariables();
                            c.Build();
                        })
                        .ConfigureLogging((context, b) =>
                        {
                            b.SetMinimumLevel(LogLevel.Debug);
                            b.AddConsole();

                            string appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                            if (!string.IsNullOrEmpty(appInsightsKey))
                            {
                                b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
                            }
                        })
                        .UseConsoleLifetime();

            var host = builder.Build();
            using (host)
            {
                await host.RunAsync();
            }
        }

函数.cs

public static async Task ProcessAsync([TimerTrigger("0 */3 * * * *")] TimerInfo timerInfo, ILogger log)
{
   //I want to read my appsettings.json here!
}

appsettings.json 示例:

{
  "AzureWebJobsDashboard": "################",
  "AzureWebJobsStorage": "################",
  "ApiUrl": "#############",
  "APPINSIGHTS_INSTRUMENTATIONKEY": "############"
}

【问题讨论】:

    标签: azure-webjobs azure-webjobssdk


    【解决方案1】:

    忽略这个 - 仍然无法正常工作

    刚刚想通了!在我的 Functions 类开始时初始化它为我修复了它:

    private readonly IConfiguration _configuration;
    public Functions(IConfiguration configuration)
    {
        _configuration = configuration;
    }
    
    public static async Task ProcessAsync([TimerTrigger("0 */3 * * * *")] TimerInfo timerInfo, ILogger log)
    {
       var conf = _configuration["ApiUrl"];
    }
    

    这可行,但会导致 WebJob 作为一个函数运行。每次运行都应在日志中的 WebJob 父函数下作为每个单独的运行进行细分。令人难以置信的沮丧。

    如果有人可以帮忙,请告诉我。

    【讨论】:

    • 您是否将“appsettings.json”的“Build Action”设置为“Content”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多