Reputation: 625
.NET Core 2.2, WebJobs SDK 3.0
I have a webjob that I configure like this :
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
HostBuilder builder = new HostBuilder();
builder.ConfigureAppConfiguration((context, configApp) => {
configApp.AddEnvironmentVariables();
configApp.AddJsonFile("appsettings.json", optional: true);
configApp.AddJsonFile($"appsettings.{environmentName}.json",optional: false);
});
builder.ConfigureHostConfiguration((configApp) => {
configApp.AddEnvironmentVariables();
configApp.AddJsonFile("appsettings.json", optional: true);
configApp.AddJsonFile($"appsettings.{environmentName}.json", optional: false);
});
I have a appsettings.test.json
file deployed to my environment and when I check my environment variable, I have indeed test
as the environment.
I can see that the file gets loaded because optional is false and I don't have any exception.
However, when my webjob starts, it doesn't use the storage connection string I have in my appsettings.test.json, it uses the one in appsettings.json
My AppInsights connection doesn't work either and I initializes it like this :
builder.ConfigureLogging((context, b) => {
b.AddConfiguration(context.Configuration.GetSection("Logging"));
Console.WriteLine($"App Insight key is {context.Configuration["ApplicationInsights:InstrumentationKey"]}");
b.AddApplicationInsightsWebJobs(o => o.InstrumentationKey = context.Configuration["ApplicationInsights:InstrumentationKey"]);
});
When the code runs, the Console shows the App Insights Key that is in the appsettings.json and not the appsettings.test.json
What can I do to force the host to use my appsettings.test.json?
Upvotes: 3
Views: 2048
Reputation: 18556
You can extend the existing configuration with additional options.
Make sure you have the correct DOTNET_ENVIRONMENT
set up as environment variable.
async static Task Main(string[] args)
{
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
builder.ConfigureServices(services =>
{
services.AddScoped<Functions>();
});
builder.ConfigureAppConfiguration((context, configurationBuilder) =>
{
configurationBuilder
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: false)
.AddEnvironmentVariables(); // I prefer environment variables to "override" appsetting files
});
IHost host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
If you are not yet on .net core 3.x, you can also do this:
```csharp
async static Task Main(string[] args)
{
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
builder.ConfigureServices(services =>
{
services.AddScoped<Functions>();
});
builder.ConfigureAppConfiguration((context, configurationBuilder) =>
{
configurationBuilder
.AddJsonFile("appsettings.json", optional: false)
.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: false)
.AddEnvironmentVariables(); // I prefer environment variables to "override" appsetting files
});
IHost host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
Full sample project: https://github.com/LXBdev/WebJob-netcore-sample
Upvotes: 1