45
loading...
This website collects cookies to deliver better user experience
Microsoft.FeatureManagement.IFeatureFilter
, override the EvaluateAsync
method add the logic to decide if feature should be enabled. Thats it! What i want to show is how do we wire up this class into the application flow and configure the flag in Azure app configurations.//The feature filter will be known my this name in the configuration.
[FilterAlias("ShortTimeFeature")]
public class SecondsFeatureFilter : Microsoft.FeatureManagement.IFeatureFilter
{
private readonly IMinuteFeaturePropertyAccessor minuteFeatureContextAccessor;
//The IMinuteFeaturePropertyAccessor is an implementation that I have used to capture first load
// time of the application. The IMinuteFeaturePropertyAccessor is loaded as a singleton in DI.
public SecondsFeatureFilter(IMinuteFeaturePropertyAccessor minuteFeatureContextAccessor)
{
this.minuteFeatureContextAccessor = minuteFeatureContextAccessor;
}
public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context)
{
//The logic is to disable the feature after configured seconds the user has used the application
//The seconds is configured as parameter in the feature flag.
var EnabledSeconds = int.Parse(context.Parameters.GetSection("EnabledSeconds").Value);
if (DateTime.Now.Subtract(minuteFeatureContextAccessor.GetStartTime()).TotalSeconds > EnabledSeconds)
{
return Task.FromResult(false);
}
return Task.FromResult(true);
}
}
public class MinuteFeturePropertyAccessor : IMinuteFeaturePropertyAccessor
{
readonly DateTime startTime;
public MinuteFeturePropertyAccessor()
{
startTime = DateTime.Now;
}
public DateTime GetStartTime()
{
return startTime;
}
}
public interface IMinuteFeaturePropertyAccessor
{
DateTime GetStartTime();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddSession();
services.AddHttpContextAccessor();
services.AddControllersWithViews();
services.AddSingleton<IMinuteFeaturePropertyAccessor, MinuteFeturePropertyAccessor>();
services.AddFeatureManagement().AddFeatureFilter<SecondsFeatureFilter>();
services.AddAzureAppConfiguration();
}
[FeatureGate("TimeLimit")]
public async Task<IActionResult> Index()
{
return View();
}