Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Feature flags can use feature filters to enable features conditionally. For more information about feature filters, see Enable conditional features with feature filters.
This guide shows you how to implement a custom feature filter in an ASP.NET Core application and use the feature filter to enable features conditionally.
Prerequisites
- The ASP.NET Core application with a Beta feature flag that you create when you complete the steps in Quickstart: Add feature flags to an ASP.NET Core app. This guide builds on that feature management quickstart.
- A custom feature filter called
Random, added to the Beta feature flag in your Azure App Configuration store. For instructions, see Add a custom feature filter.
Implement a custom feature filter
In your App Configuration store, the Beta feature flag has a custom feature filter named Random. That filter has a Percentage parameter. To implement the feature filter, you enable the Beta feature flag based on the chance defined by the Percentage parameter.
Go to the folder that contains the ASP.NET Core application project from the feature management quickstart listed in Prerequisites.
Add a RandomFilter.cs file that contains the following code:
using Microsoft.FeatureManagement; namespace TestAppConfig { [FilterAlias("Random")] public class RandomFilter : IFeatureFilter { private readonly Random _random; public RandomFilter() { _random = new Random(); } public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context) { int percentage = context.Parameters.GetSection("Percentage").Get<int>(); int randomNumber = _random.Next(100); return Task.FromResult(randomNumber <= percentage); } } }The
RandomFilterclass in the preceding code implements theIFeatureFilterinterface from theMicrosoft.FeatureManagementlibrary. TheIFeatureFilterinterface has a single method namedEvaluateAsync, which is called whenever a feature flag is evaluated. InEvaluateAsync, a feature filter enables a feature flag by returningtrue.In this code, the
RandomFilterclass is decorated withFilterAliasAttribute, which gives your filter the aliasRandom. That alias matches the filter name in the Beta feature flag in App Configuration.Open the Program.cs file. Register
RandomFilterby adding a call to theAddFeatureFiltermethod, as shown in the following code:// Existing code in Program.cs // ... ... // Add feature management to the container of services. builder.Services.AddFeatureManagement() .AddFeatureFilter<RandomFilter>(); // The rest of the existing code in Program.cs // ... ...
Apply the feature filter
Build your app by using the
dotnet buildcommand. Then run it by usingdotnet run.In the output of the
dotnet runcommand, find a URL that the web app is listening on. Open a browser and go to that URL.Refresh the browser a few times. The Beta menu sometimes appears on the webpage, and sometimes it doesn't. The Beta feature flag isn't being manually turned on and off. The variation is caused by the random number that the
RandomFilterimplementation ofEvaluateAsyncgenerates when the feature flag is evaluated.
Next steps
To find out more about built-in feature filters, continue to the following documents:
For the full feature rundown of the .NET feature management library, continue to the following document: