Azure App Configuration Label

Rajarajacholan Krishnamurthy 21 Reputation points
2022-08-30T12:59:08.497+00:00

I wanted to read values per environment from my MVC application and here is the code snippet:

program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
var azAppConfigSettings = config.Build();
var azAppConfigConnection = azAppConfigSettings["AppConfig"];
if (!string.IsNullOrEmpty(azAppConfigConnection))
{
// Use the connection string if it is available.
config.AddAzureAppConfiguration(options =>
{
options.Connect(azAppConfigConnection)
.Select(KeyFilter.Any, context.HostingEnvironment.EnvironmentName)
.ConfigureRefresh(refresh =>
{
// All configuration values will be refreshed if the sentinel key changes.
refresh.Register("TestApp:Settings:Sentinel", refreshAll: true);
refresh.Register("TestApp:Settings:Message").SetCacheExpiration(TimeSpan.FromDays(30));
});
});
}
else if (Uri.TryCreate(azAppConfigSettings["Endpoints:AppConfig"], UriKind.Absolute, out var endpoint))
{
// Use Azure Active Directory authentication.
// The identity of this app should be assigned 'App Configuration Data Reader' or 'App Configuration Data Owner' role in App Configuration.
// For more information, please visit https://aka.ms/vs/azure-app-configuration/concept-enable-rbac
config.AddAzureAppConfiguration(options =>
{
options.Connect(endpoint, new DefaultAzureCredential())
.ConfigureRefresh(refresh =>
{
// All configuration values will be refreshed if the sentinel key changes.
refresh.Register("TestApp:Settings:Sentinel", refreshAll: true);
});
});
}
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

ConfigController.cs

public class ConfigController : ControllerBase
{
private readonly ILogger<ConfigController> _logger;
private readonly IConfiguration _configuration;
private readonly IHostingEnvironment _hostingEnvironment;

    public ConfigController(IHostingEnvironment hostingEnvironment, IConfiguration configuration, ILogger<ConfigController> logger)  
    {  
        _logger = logger;  
        _configuration = configuration;  
        _hostingEnvironment = hostingEnvironment;  
    }  

      
    [HttpGet]  
    public string Get(string key = "TestApp:Settings:Message")  
    {  
        key = key + ":" + _hostingEnvironment.EnvironmentName;  
        return _configuration.GetSection(key).Value.ToString();  
    }  
}  

It is always returning the value from the key that does not have a label, though EnvironmentName is set to Development and there is a matching label in Azure App Config created. Can someone help me what I am missing here please? There are three keys in Azure App Config, one without a label, one with Development and another with Production labels.

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

2 answers

Sort by: Most helpful
  1. Rajarajacholan Krishnamurthy 21 Reputation points
    2022-09-19T12:51:49.54+00:00

    Thanks for the follow-up!

    Changing the order of services.AddController and services.Configure in ConfigureServices method of startup class resolved. But every request often hits Azure App Config and sometime we exhaust over using it. But I have been trying something differently:

    I want the requests to hit the Azure App Config only once in a day and when changes are detected, they should be fetched immediately within 60 seconds. I would appreciate if someone could help.

    // All configuration values will be refreshed if the sentinel key changes.
    refresh.Register("TestApp:Settings:Sentinel", refreshAll: true)
    .SetCacheExpiration(new TimeSpan(0, 0, 60)); ;

                                   refresh.Register("TestApp:Settings:Message", context.HostingEnvironment.EnvironmentName)  
                                       .SetCacheExpiration(TimeSpan.FromDays(1));
    
    1 person found this answer helpful.

  2. MuthuKumaranMurugaachari-MSFT 22,261 Reputation points
    2022-09-07T15:45:27.91+00:00

    @Rajarajacholan Krishnamurthy Thank you for reaching out to Microsoft Q&A. Sorry for the delay in response. I would like to check with you if you are still facing the issue or figured out already. Based on your statement, you already validated the environment name, labels, but still app configuration loads value with no label.

    Checking the code snippet, you used ConfigureWebHostDefaults() method after ConfigureAppConfiguration() (ConfigureWebHostDefaults should be called before app config as per docs). So I suggest you call ConfigureAppConfiguration() first to get the config and use it to build IWebHostBuilder. Can you please try changing the code snippet as below and validate?

    public static IHostBuilder CreateHostBuilder(string[] args) =>  
            Host.CreateDefaultBuilder(args)  
            .ConfigureWebHostDefaults(webBuilder =>  
            webBuilder.ConfigureAppConfiguration((context, config) =>  
            {  
                var azAppConfigSettings = config.Build();  
                var azAppConfigConnection = azAppConfigSettings["AppConfig"];  
                if (!string.IsNullOrEmpty(azAppConfigConnection))  
                {  
                    // Use the connection string if it is available.  
                    config.AddAzureAppConfiguration(options =>  
                    {  
                        options.Connect(azAppConfigConnection)  
                        .Select(KeyFilter.Any, context.HostingEnvironment.EnvironmentName)  
                        .ConfigureRefresh(refresh =>  
                        {  
                            // All configuration values will be refreshed if the sentinel key changes.  
                            refresh.Register("TestApp:Settings:Sentinel", refreshAll: true);  
                            refresh.Register("TestApp:Settings:Message").SetCacheExpiration(TimeSpan.FromDays(30));  
                        });  
                    });  
                }  
                else if (Uri.TryCreate(azAppConfigSettings["Endpoints:AppConfig"], UriKind.Absolute, out var endpoint))  
                {  
                    // Use Azure Active Directory authentication.  
                    // The identity of this app should be assigned 'App Configuration Data Reader' or 'App Configuration Data Owner' role in App Configuration.  
                    // For more information, please visit https://aka.ms/vs/azure-app-configuration/concept-enable-rbac  
                    config.AddAzureAppConfiguration(options =>  
                    {  
                        options.Connect(endpoint, new DefaultAzureCredential())  
                        .ConfigureRefresh(refresh =>  
                        {  
                            // All configuration values will be refreshed if the sentinel key changes.  
                            refresh.Register("TestApp:Settings:Sentinel", refreshAll: true);  
                        });  
                    });  
                }  
            })  
            .UseStartup<Startup>());   
    

    Refer Load configuration values with a specified label for more info on this sample and its usage. Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community.

    0 comments No comments