Azure App Config and appsettings.jsons

Rajarajacholan Krishnamurthy 21 Reputation points
2023-07-05T09:59:06.52+00:00

How can I use appsettings.json when Azure App Config can't be reached. I have a situation where minimum number of retries does not help and I want to use appsettings.json of the application itself.

Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
229 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 70,421 Reputation points
    2023-07-05T10:41:55.2266667+00:00

    @Rajarajacholan Krishnamurthy Thanks for reaching out.

    If you are unable to reach Azure App Configuration and want to use the appsettings.json file of your application instead, you can modify your code to read the configuration values from the appsettings.json file instead of Azure App Configuration.

    You can read configuration values from the appsettings.json file in a .NET Core application:

    Add the Microsoft.Extensions.Configuration.Json NuGet package to your project.

    In your Program.cs file, add the following code to create a ConfigurationBuilder object and load the appsettings.json file:

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.Json;
    
    // ...
    
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    IConfigurationRoot configuration = builder.Build();
    

    In your code, you can now access configuration values using the IConfiguration interface. For example:

    string connectionString = configuration.GetConnectionString("MyConnectionString");

    This code reads the value of the MyConnectionString connection string from the appsettings.json file.

    By default, the appsettings.json file is included in the output directory of your application when you build it. This means that you can modify the appsettings.json file on the server where your application is running, and the changes will take effect immediately without requiring a rebuild or redeployment of your application.

    However, keep in mind that this approach does not provide the same level of centralized management and versioning that Azure App Configuration provides. You can now get the values from application.json if your custom code if you are not able to retrieve the value from Azure app configuration.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.