Search code examples
c#asp.net-coreterraformazure-web-app-serviceappsettings

Issue with azure app service (linux) environment variable in ASP.NET Core app


I have an ASP.NET Core app with following setting files:

appsettings.json:

...

"AzureOpenAI": {
    "EndPoint": "https://PRODUCTION.openai.azure.com/",
    "ApiKey": "PRODUCTION-API-KEY"
},
"ServiceBus": {
    "ConnectionString": "PRODUCTION-CONNECTION-STRING",
    "QueueNames": {
      "Files": "files",
      "SessionCallBack": "sessionCallBack"
   }
}
...

appsettings.staging.json:

...
"AzureOpenAI": {
    "EndPoint": "https://STAGING.openai.azure.com/",
    "ApiKey": "STAGING-API-KEY"
},
"ServiceBus": {
    "ConnectionString": "STAGING-CONNECTION-STRING"
}
...

And this is my program class:

...
var env = builder.Environment.EnvironmentName;
var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: true)
    .AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true)
    .AddCommandLine(args)
    .Build();

Console.WriteLine($"Environment: {env}"); // Staging

// This is working
var AzureOpenAIConfig = _config.GetRequiredSection("AzureOpenAI").Get<AzureOpenAIConfig>(); 

// Service Bus, not working!
var ServiceBusConfig = config.GetRequiredSection("ServiceBus").Get<ServiceBusConfig>()!;
Console.WriteLine($"ServiceBus:ConnectionString '{ServiceBusConfig.ConnectionString}'");
// Output: ServiceBus:ConnectionString 'STAGING-CONNECTION-STRING'
...

My terraform module:

...
resource "azurerm_linux_web_app" "app" {
  name                = "app-${local.appName}"
  location            = var.location
  resource_group_name = module.rg.resourcegroup_name
  service_plan_id     = azurerm_service_plan.asp.id

  identity {
    type = "SystemAssigned"
  }

  site_config {
    always_on = true

    application_stack {
      dotnet_version = "8.0"
    }
  }

  app_settings = {
    "ASPNETCORE_ENVIRONMENT"         = var.env == "prod" ? "Production" : "Staging"
    "AzureOpenAI__EndPoint"          = azurerm_cognitive_account.open_ai.endpoint
    "AzureOpenAI__ApiKey"            = "@Microsoft.KeyVault(VaultName=${module.kv.name};SecretName=AzureOpenAI-ApiKey)"
    "ServiceBus__ConnectionString"     = "@Microsoft.KeyVault(VaultName=${module.kv.name};SecretName=ServiceBus-ConnectionString)"
    "AzureMonitor__ConnectionString" =
}
...

Environment variables:

enter image description here

enter image description here

So after deployment I can see KV-Secrets in app configuration and their value are valid as well in kv. The AzureOpenAI:EndPoint and AzureOpenAI:ApiKey are working well but not for service bus, and it's really weird!


Solution

  • Finally I found the answer! I should add one line to my program class:

    var env = builder.Environment.EnvironmentName;
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: true)
        .AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables() // this line was missing
        .AddCommandLine(args)
        .Build();