Feature Flags in .NET Core with Azure

In the previous article, we have been checked with the feature flag used in the application configuration. Here, we check with the azure side.

Introduction

In order to dynamically control the availability of a new web page in an ASP.NET Core project without having to restart or reinstall it, you will need to create a feature flag in Azure project Configuration.

Prerequisites

Required the App Configuration resource in Azure.

Here is the step for creating the app configuration in Azure.

  1. Search for the App Configuration resource in the Azure search bar.
  2. Click on the app configuration resource.
  3. Click on the Create button to create the app configuration resource in Azure. Please fill in the required details if you haven’t already created the subscription and resource groups, so first you need to create them and use them here. Here I have associated the pricing tier as free for the demo purpose. If you require the standard pricing tier, then you need to select the standard tier.
    Create button
  4. Click on the Next, Advanced > button for the next step and fill in the necessary details, or if you have changed any configuration.
    Next Advanced
  5. Click on the Next: Networking > button for the next step and fill in the necessary details or if you have changed any configuration. If you want to change anything on the Basic tab, then you need to click on the < Previous button for the back step and change any configuration. In the Networking tab, you need to choose whether your app configuration requires access via public or private. For the free tier, private access is not available.
    Networking
  6. Click on the Next: Encryption > button for the next step and fill in the necessary details, or if you have changed any configuration. If you want to change anything on the Networking tab, then you need to click on the < Previous button for the back step and change any configuration. In the free tier, customer managed keys are not available; if you want to use the standard tier, they're available.
    Encryption

Here is the encryption configuration for the standard tier.

Encryption configuration

Fill in the necessary details, and then click on the Next: Tags > button.

  1. Click on the Next: Tags > button for the next step and fill in the necessary details or if you have changed any configuration. If you want to change anything on the encryption tab, then you need to click on the < Previous button for the back step and change any configuration.
    Tags
  2. Click on the Next: Review + Create > button for the next step and fill in the necessary details or if you have changed any configuration. If you want to change anything on the Tabs tab, then you need to click on the < Previous button for the back step and change any configuration.
    Create
  3. Click on the Create button to create the App Configuration resource in Azure. If you want to change any configuration, then click on the < Previous button.
  4. After clicking on the Create button, Azure will start creating the resources, as we have seen in the notification section of the Azure Portal.
  5. Once the resource has been successfully created, you will see the screen below.
    App Configuration
  6. Click on the Go to resource button, and it will redirect to the App Configuration resource.

Here is the overview page for the app configuration resource.

Go to resource

To create the feature flag configuration, follow the below steps.

  1. You need to find the feature manager and click on that menu.
    Feature manager
  2. Click on the Create button to create the feature flag.
    Feature flag
  3. Once you have clicked on the Create button, below the screen you will see. Fill in the necessary fields and click on the Apply button.
    Apply button
  4. Once the feature flag is created, below is the screen you will see.
    Feature flag is created
  5. Here is the create feature flag for the filter. While creating the feature flag, you need to check the Use feature filters, then click on the Create button. After that, it will open another window for creating the filter, and you need to provide the necessary details. Once everything is configured, click on Apply to create the feature flag.
    Feature filters
    Create new filters
    Details
  6. Once the feature flag with filter is created, you will see the below screen.
    Feature flag with filter
  7. If you want to edit any of the feature flag values, then it's also possible here. Click on the horizontal 3 dots on the right side of the feature list page. Once you click on the 3 dots, it will open the popup menu, and if you click on edit, it will open a window for you to update or change the configuration value.
    Feature flag values
  8. If you want to click on Advance Edit, then it will open a JSON format window to change the configuration value. Once you have changed the configuration value, click on the Apply button. If you want to discard your changes, then you need to click on the Discard button.
    Advance Edit
  9. If you want to delete any of the feature flags, then it is also possible here. Click on the 3 dots on the right side and click on the Delete button.
    Delete button
  10. If you want to enable or disable any of the feature flags, you need to click on the switch.
    Enable or disable

Let’s check on the .Net Core side.

You need to install the below packages in the .net core application.

  1. Microsoft.Azure.AppConfiguration.AspNetCore
  2. Microsoft.FeatureManagement.AspNetCore

Once you have been installed, you will need to register in the Program.cs file.

builder.Configuration.AddAzureAppConfiguration(options =>
{
    options.Connect(connectionString)

    // Configure to reload configuration if the registered sentinel key is modified
    .ConfigureRefresh(refreshOptions =>
         refreshOptions.Register("TestFeature", refreshAll: true));

    // Load all feature flags with no label
    options.UseFeatureFlags();
}).Build();

builder.Services.AddAzureAppConfiguration();

builder.Services.AddFeatureManagement()
    .AddFeatureFilter<JaiminCustomFilter>();

The below mention is added to the middleware to use the Azure app configuration on the application side.

app.UseAzureAppConfiguration();

Here is the custom filter class (JaiminCustomFilter.cs).

[FilterAlias("JaiminCustomFilter")]
public class JaiminCustomFilter : IFeatureFilter
{
    public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context)
    {
        var settings = context.Parameters.Get<ValueSettings>();
        return Task.FromResult(settings.Value == 50);
    }
}

Here is the value settings class (ValueSettings.cs).

public class ValueSettings
{
    public int Value { get; set; }
}

Here is the code for whether the feature is enabled or disabled.

var testFeature = await _featureManager.IsEnabledAsync("TestFeature");
var beta = await _featureManager.IsEnabledAsync("Beta");

Here is the full code for getting value from Azure and giving the result as in the API response.

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private readonly IFeatureManager _featureManager;

    public ValuesController(IFeatureManager featureManager)
    {
        _featureManager = featureManager;
    }

    [HttpGet]
    public async Task<IActionResult> GetAsync()
    {
        var testFeature = await _featureManager.IsEnabledAsync("TestFeature");
        
        var response = new StringBuilder();
        response.AppendLine($"{nameof(testFeature)} {testFeature} ");

        if (testFeature)
        {
            response.AppendLine($"{nameof(testFeature)} {DateTime.UtcNow:dd:MM:yyyy HH:mm:ss.fff} ");
        }

        var beta = await _featureManager.IsEnabledAsync("Beta");
        response.AppendLine($"{nameof(beta)} {beta} ");

        if (beta)
        {
            response.AppendLine($"{nameof(beta)} {DateTime.UtcNow:dd:MM:yyyy HH:mm:ss.fff} ");
        }

        return Ok(response.ToString());
    }
}

Let’s try to run the application and check the response from the API.

API

Let's make changes in the Azure app configuration side and after executing the API verify the response of the API.

Azure app configuration

API values

Let’s try to change the filter value in the Azure app configuration.

Filter value

Namespace

Execute

We learned the new technique and evolved together.

Happy coding!


Similar Articles