Reputation: 5941
Is that possible to read values from appsettings.json before configuration.build ?
i need to get endpoint, clientId, secret and tenant to bind configuration with azure app configuration
Configuration = builder.AddAzureAppConfiguration(options =>
{
//here i need to get some values from appsettings.json
}).Build()
Upvotes: 3
Views: 1729
Reputation: 504
You can use a ConfigurationBuilder
as such:
var builder = new ConfigurationBuilder()
.SetBasePath("yourJsonPath")
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
var config = builder.Build();
then you can retrieve the value you need:
config.GetSection("AppConfig:Endpoint").Value
or just
config.GetSection("ClientId").Value
Depending on how your JSON is built.
Upvotes: 2
Reputation: 730
It's not possible.
Here are two examples of a scenario similar to yours in the Azure App Configuration GitHub examples:
The example shows that the ConfigurationBuilder
is built to obtain an intermediate IConfiguration
that is then used to provide a connection string for adding Azure App Configuration to the builder.
In the ASP.NET Core web application example the same pattern is used with the framework provided IConfigurationBuilder
Upvotes: 2
Reputation: 11271
I'm not sure about this, as I usually use the WebHostBuilder, but I think it should be something like:
var settings = builder.Build();
Configuration = builder.AddAzureAppConfiguration(options =>
{
options.Connect(settings["ConnectionStrings:AppConfig"])
}).Build();
edit: note I assumed you already added appsettings.json to the builder. If not, you need to add
var env = [something like]hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
Upvotes: 1