Press "Enter" to skip to content

ASP.NET Core Feature Management

Tahmini okuma süresi: 9 dakika

Feature Management provides feature management in .NET Core applications. It allows the management and querying of the active / passive status of the features of the application. For example, you can ensure that a feature you have just developed is active in a certain date range. To give another example, you can ensure that a feature you have developed is active with a certain percentage. Like A/B testing.

Let’s test it by practicing. First, we need to install the package below.

dotnet add package Microsoft.FeatureManagement.AspNetCore -version 2.0.0

Let’s define our features in enum.

Let’s activate the Feature Management in the Startup.

Because Feature Management reads features from IConfiguration, we can define features in environment variable, commandline argument or appsettings. Or, if you have a custom provider, you can define your features in the related source. We will use appsettings in our example. Let’s define the feature named FeatureA.

We need to inject the “IFeatureManager” interface to query if the features are activated. By using the “IsEnabledAsync” method of this interface, we can query as follows if the “FeatureA” feature is activated.

TimeWindowFilter

If we want the feature to be active in a certain date range, we can use the internal “TimeWindowFilter”. For this, we need to activate this filter in the Startup.

Let’s define the feature named FeatureB in appsettings.

UTC Date is used as the date format. If you only give the Start parameter, the feature will be active from this date. If you only give the End parameter, the feature will be active until this date.

Again, using the “IsEnabledAsync” method of the “IFeatureManager” interface, we can query if the “FeatureB” feature gets activated as follows.

PercentageFilter

If we want the feature to be active with a certain percentage, we can use the internal “PercentageFilter”. For this, we need to activate this filter in the Startup.

Let’s define the feature named FeatureC in appsettings.

This feature will be active in 80% of the requests.

Again, using the “IsEnabledAsync” method of the “IFeatureManager” interface, we can query if the “FeatureC” feature gets activated as follows.

Custom Filter

Apart from these filters, you can also write your own filters. For example, a filter can be written as below for the feature to be active on a mobile device.

Let’s define the feature named FeatureD in appsettings.

Let’s create “MobileFilterSettings” class to access filter parameters.

Let’s write our special filter named “MobileFilter” as follows.

We need to activate this filter in the Startup.

Again, using the “IsEnabledAsync” method of the “IFeatureManager” interface, we can query if “FeatureD” feature gets activated as follows.

Mvc Action Filter Attribute

Instead of using the IFeatureManager interface, you can set the activation of the attribute based Controller or Action, thanks to ActionFilter.

Razor Tag Helper

You can use the feature razor tag if you want to display the content in the View according to the activation status of the feature.

Middleware

If you want to add middleware according to the activation status of the feature, you can use the UseMiddlewareForFeature method.

You can access the sample application from Github.

Good luck.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *