Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using IConfiguration, [value] works but IsSettingEnabled for the same returns false

In the particular project I am working on, I am using the same pattern as I use in other projects successfully. However, with this one I am unable to successfully use IConfiguration’s IsSettingEnabled.

Setup.cs

class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
    }

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        var context = builder.GetContext();

        builder.ConfigurationBuilder
            .AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false);
    }
}

appsettings.Development.json

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
    "ConfigName": "myconfig"
}

TheFunction.cs

public class TheFunction
{
    private IConfiguration _configuration = null;

    public TheFunction(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [FunctionName("TheFunction")]
    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        string configName = "ConfigName";
        string configValue = _configuration[configName];
        bool configValueExists = _configuration.IsSettingEnabled(configName);
    }

When the code runs, configValue is "myconfig" as expected. However, configValueExists is False.

Is there something I missed in the setup? It’s like whatever Dictionary IsSettingEnabled is using has not been built, and that the [] operator uses a different method of getting the value.

>Solution :

Based on the source code Azure’s IsSettingEnabled checks if settings value is not empty and can be interpreted as true boolean:

public static bool IsSettingEnabled(this IConfiguration configuration, string settingName)
{
    // check the target setting and return false (disabled) if the value exists
    // and is "falsey"
    string value = configuration[settingName];
    if (!string.IsNullOrEmpty(value) &&
        (string.Compare(value, "1", StringComparison.OrdinalIgnoreCase) == 0 ||
            string.Compare(value, "true", StringComparison.OrdinalIgnoreCase) == 0))
    {
        return true;
    }
            
    return false;
}

And "myconfig" definitely can not be interpreted as truthful in this context.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading