【发布时间】: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