I've created an Azure AppConfiguration store and get the Access Keys (EndPoint, Id, Secret, ConnectionString).
Now from my .NET Core WebAPI app, I want to read the configurations from Azure. I have tried many approaches that I found online but none of them works.
I already added references to the required Nuget libraries.
My appsettings.json
:
{
"ConnectionString:AppConfig": "Endpoint=https://somewhere.azconfig.io;Id=Hidden;Secret=Hidden",
"EndPoint": "https://somewhere.azconfig.io"
}
In my Program.cs
I have:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var settings = config.Build();
config.AddAzureAppConfiguration(options =>
{
options.Connect(settings["ConnectionStrings:AppConfig"]);
});
})
.UseStartup<Startup>();
Then in my Startup.cs
constructor:
public Startup(
IConfiguration configuration, ---> checked this but no Azure config data
IHostingEnvironment hostingEnv
)
{
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
//this.Config = configuration;
var builder = new ConfigurationBuilder()
.SetBasePath(hostingEnv.ContentRootPath)
.AddJsonFile(...)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
And this is where I read the configurations, but all the values are null
public void ConfigureServices(IServiceCollection services)
{
var dbConnection = Configuration.GetSection("MyApp:Key1:Key2").Value; ---> get null
services.AddDbContext<MyDbContext>(options => options.UseSqlServer(dbConnection));
_jwtLifespan = Configuration.GetValue<int>("MyApp:Key3"); ----> get null
_jwtSecretKey = Configuration.GetSection("MyApp:Key4").Value; ----> get null
.....
}
Anyone please check what I'm missing here and suggest me a working solution. Many thanks!