Usage of Azure App Configuration's Feature Flags in Azure Functions

走远了吗. 提交于 2021-01-29 19:12:40

问题


I'm working on exploring the following 2 features of Azure App Configuration in Azure Function's Http Trigger

  1. Externalizing the App Settings
  2. Feature Flags

Below is how i'm getting the reference of the configuration

So, when I use _configuration["SomeAppSettingKey"], I'm able to retrieve the value. So, I'm able to achieve #1 feature mentioned above.

My Question is, How do we retrieve the Feature Flag information? I have tried the below ways.

I would appreciate if someone could help me in understanding how to retrieve it in Azure Functions (I'm using V3)? A Sample code or any reference to documentation would be helpful.

Thanks.

Update1: I can deserialize the json content as shown below. But, is this is the right approach?

Where FeatureManager is a class that I have defined as shown below.


回答1:


all you need is to call UseFeatureFlags() function as part of AddAzureAppConfiguration to let the App Configuration provider know you want to use feature flags. An example can be found following the link below. It uses the FunctionsStartup and dependency injection (DI) of Azure Functions. An instance of a feature manager is put into the DI.

https://github.com/Azure/AppConfiguration/blob/master/examples/DotNetCore/AzureFunction/FunctionApp/Startup.cs

The link below shows how you can obtain the instance of IFeatureManagerSnapshot from DI and use it as part of your Azure Functions call.

https://github.com/Azure/AppConfiguration/blob/master/examples/DotNetCore/AzureFunction/FunctionApp/ShowBetaFeature.cs




回答2:


Deserialize JSON is not a good idea, every time you will add new key you need to modify your class.

private static IConfiguration Configuration { set; get; }

static Function1()
{
    var builder = new ConfigurationBuilder();
    builder.AddAzureAppConfiguration(Environment.GetEnvironmentVariable("ConnectionString"));
    Configuration = builder.Build();
}

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string keyName = "TestApp:Settings:Message";
    string message = Configuration[keyName];

    return message != null
        ? (ActionResult)new OkObjectResult(message)
        : new BadRequestObjectResult($"Please create a key-value with the key '{keyName}' in App Configuration.");
}


来源:https://stackoverflow.com/questions/61181243/usage-of-azure-app-configurations-feature-flags-in-azure-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!