C
C#2y ago
Alexicon

❔ Is there a better way to insert a configuration source before the appsettings files?

I have an AspNetCore web API where I am using the appsettings.json and appsettings.Development.json, which are automatically added to the configuration manager when you call
WebApplication.CreateBuilder(args);
WebApplication.CreateBuilder(args);
however I am also adding a azure key vault as another configuration source like this:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddAzureKeyVault(...);
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddAzureKeyVault(...);
The issue is that the key vault and appsettings.Development both contain the same connection string key but with different values. Because the appsettings are added before the azure key vault, the key vault connection string is always used, however I want to use the connection string from the appsettings.Development when its available. So the obvious solution is to do the following:
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddAzureKeyVault(...);

if (builder.Environment.IsDevelopment())
{
builder.Configuration.AddJsonFile("appsettings.Development.json");
}
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddAzureKeyVault(...);

if (builder.Environment.IsDevelopment())
{
builder.Configuration.AddJsonFile("appsettings.Development.json");
}
And this works but I was wondering if there is a more elegant or correct way that I could simply insert the azure key vault before the appsettings configuration. I suspect there isnt anything better based on what I can find but I figured I would ask just in case.
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.