3

I'm struggling to read any variables from my appsettings.json file in an Azure Webjob SDK 3.0 project.

I am using:

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

I've tried the following:

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

but this returns null and fails to read. I've checked the documentation and other questions on SO, but currently it's like finding a needle in a haystack.

Program.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();
            }
        }

Functions.cs

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

appsettings.json example:

{
  "AzureWebJobsDashboard": "################",
  "AzureWebJobsStorage": "################",
  "ApiUrl": "#############",
  "APPINSIGHTS_INSTRUMENTATIONKEY": "############"
}
Davy-F
  • 123
  • 1
  • 2
  • 12

1 Answers1

0

IGNORE THIS - STILL not working

Just figured it out! Initialising this at the start of my Functions class fixed it for me:

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"];
}

This works, but causes the WebJob to run as one function. Each run should be broken out as each individual run, under the WebJob function parent in the logs. Incredibly frustrating.

If anybody can help, please let me know.

Davy-F
  • 123
  • 1
  • 2
  • 12