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": "############"
}