0

I have a Key-vault storage connected to my Azure functions app. I can access values as environment variables when put references into application settings:

Example of app configuratiom

But I also have a custom configuration file in Json format. I read it in my Startup class:

public override void Configure(IFunctionsHostBuilder builder)
{
  var envName = Environment.GetEnvironmentVariable("ENVIRONMENT_NAME");
  var config = new ConfigurationBuilder()
      .SetBasePath(builder.GetContext().ApplicationRootPath)
      .AddJsonFile($"server.settings.{envName}.json", optional: false, reloadOnChange: false)
      .AddEnvironmentVariables()
  .Build();
  MyConfig mainConf = new();
  config.Bind("MyConfig", mainConf);
  builder.Services.AddSingleton(mainConf);
}

One item in this file refers to the same secret:

"SecretDir": "@Microsoft.KeyVault(SecretUri=https://...)"

This value does not become interpolated. I see the reference instead of referenced value. How can I fix that?

Infarch
  • 35
  • 4
  • Does this answer your question? [Using KeyVault secrets to override appsettings in Azure App Service and locally](https://stackoverflow.com/questions/68224000/using-keyvault-secrets-to-override-appsettings-in-azure-app-service-and-locally) – Silvan Jul 03 '23 at 20:18
  • Silvan, not exactly what I need. References work in application settings, but my goal is to make them working in conditional Json file. Following the naming pattern allows me to fill in values in my config, e.g. "Section--Subsection--Name" results in "Section.Subsection.Name" configuration item. OK at development stage when I can create Key-vault items by self. But later the application will be connected to centralized vault which is out of my control. There also is a security issue. Even If I need just one value from vault, all secrets will be imported into my config. – Infarch Jul 04 '23 at 06:54
  • Ah sorry, I misunderstood your question. I don't think what you are trying to do with a custom file. But why not using the `appsettings.Production.json` file? – Silvan Jul 04 '23 at 16:39
  • @Silvan, but what is the difference? I can rename my file to "appsettings.{envName}.json", but it still will be a Json file. Do you mean that there is some naming conventions making such difference? Maybe I just do not understand your idea... Could you please give me some links for reading? – Infarch Jul 05 '23 at 07:05
  • In Configure menthd , add `.AddJsonFileConfigurationSource($"YourCustomjsonfile.{envName}.json", optional: false, reloadOnChange: false)` and try once. – Harshitha Jul 07 '23 at 08:14
  • @Harshitha, What extension the method comes from? I do not see it in "Microsoft.Configuration.Extensions" and also Google cannot find it. – Infarch Jul 07 '23 at 11:17
  • I think the problem is that you are expecting this to be working locally but this only works when the app is published. – Silvan Jul 07 '23 at 20:03
  • Silvan, No, I tested it in Azure. – Infarch Jul 08 '23 at 18:42

0 Answers0