In my previous blog, I discussed feature flag and how this can be implemented using simple Boolean flag. Please refer to the below link for the same.
In this tutorial, we are going to discuss the below topics
- Advanced Feature Flag
- Custom Feature Flag
In the previous blog, we have seen that feature flagging is implemented using a simple Boolean flag.
The source code can be downloaded from the GitHub
Advanced Feature flag
Let us consider a scenario, where we have consumed a weather forecast API, and by that time, a new API has been released. How this can be included? Let us see how the Advanced Feature flag would help us in this scenario.
I have introduced a new property in Weatherforecast.cs class for explaining this Advanced Feature flag
//New Feature for advanced Advanced feature flag
public string? SnowPercentage { get; set; }
In WeatherforecastController.cs, Let us forecast the existing method to create as an Old algorithm method as below
private async Task<IEnumerable<WeatherForecast>> OldForecastAlgorithm()
{
//Feature Flag - this needs to be configure in AppSettings.json
var isTemperatureFEnabled = await _featureManager.IsEnabledAsync("TemperatureF");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
//check whether the Feature Flag is enabled
TemperatureF = isTemperatureFEnabled ? (int)(Random.Shared.Next(-20, 55) * 1.8) + 32 : null,
//To Identify the old algorithm, "OLD" has been suffixed
SnowPercentage = $"{Random.Shared.Next(0, 100)} % - OLD",
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
To identify the Old Algorithm, in Summary, I have suffixed "OLD".
For demo purpose, let me replicate the OldForecastAlgorithm method logic into a new method as below
private async Task<IEnumerable<WeatherForecast>> NewForecastAlgorithm()
{
//Feature Flag - this needs to be configure in AppSettings.json
var isTemperatureFEnabled = await _featureManager.IsEnabledAsync("TemperatureF");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
//check whether the Feature Flag is enabled
TemperatureF = isTemperatureFEnabled ? (int)(Random.Shared.Next(-20, 55) * 1.8) + 32 : null,
//To Identify the New algorithm, "NEW" has been suffixed
SnowPercentage = $"{Random.Shared.Next(0, 100)} % - NEW",
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
To identify the new algorithm, I have suffixed “NEW” in SnowPercentage property.
Now, let us go ahead and add a new feature flag for the new algorithm in GetNewFeature() endpoint in WeatherforecastController.cs
var newAlgorithm = await _featureManager.IsEnabledAsync("NewAlgorithmEnabled");
The updated GetNewFeature() endpoint looks like below.
[HttpGet("newfeature")]
[FeatureGate("NewFeature")]
public async Task<IEnumerable<WeatherForecast>> GetNewFeature()
{
var newAlgorithm = await _featureManager.IsEnabledAsync("NewAlgorithmEnabled");
return newAlgorithm? await NewForecastAlgorithm() : await OldForecastAlgorithm();
}
So far so good. Now let us look at the Appsettings.json file and make sure that new feature flag for new algorithm has been added.
"FeatureManagement": {
"TemperatureF": false,
"NewFeature": true,
"NewAlgorithmEnabled": {
"EnabledFor": [
{
"Name": "Microsoft.Percentage",
"Parameters": {
"Value": 50
}
}
]
}
}
Here “NewAlgorithEnabled” is the settings we are configuring to implement advanced feature flag. So far, we have played around with boolean flags. Here, "EnabledFor" is a property that take a list of parameters to configure the features. “Microsoft.Percentage” is built-in with feature management library. The “Value” which makes the feature flag decision.

Join the conversation! Your thoughts help the community grow.