Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Azure App Service application settings for ASP.NET Core 3.1?

This ASP.NET Core 3.1 application works fine on the local machine, but when hosted in Azure App Service it does not use the configuration variables set under "Application settings".

The following variables have been created inside of the App Service configuration with the same values set in appsettings.json:

  • SearchServiceName
  • SearchServiceQueryApiKey

How can the controller file method below be changed to use the appsettings.json file locally, and the Azure App service configuration settings in the cloud? appsettings.json is not being included in the repository in Azure DevOps that is built and deployed to Azure App Service.

Controller file method:

private void InitSearch()

{
    // Create a configuration using the appsettings file.
    _builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
    _configuration = _builder.Build();

    // Pull the values from the appsettings.json file.
    string searchServiceName = _configuration["SearchServiceName"];
    string queryApiKey = _configuration["SearchServiceQueryApiKey"];

    // Create a service and index client.
    _serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(queryApiKey));
    _indexClient = _serviceClient.Indexes.GetClient("example-index");
}

appsettings.json

{
  "SearchServiceName": "example-search-service",
  "SearchServiceQueryApiKey": "example-query-api-key",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}
like image 868
crayden Avatar asked Oct 18 '25 20:10

crayden


1 Answers

The configuration settings set in an Azure App Service are provided to the app itself using environment variables. With that in mind, you can just add the environment-variable provider to your ConfigurationBuilder, like this:

_builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables();

See Override app configuration using the Azure Portal for more information.

like image 178
Kirk Larkin Avatar answered Oct 20 '25 10:10

Kirk Larkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!