DEV Community

Luis Beltran
Luis Beltran

Posted on

(Azure App Service) Feature Flags in C#/Blazor App

This publication is part of the C# Advent Calendar 2024. Have a look at interesting articles about C# created by the community.

Feature flags, also known as feature toggles, are a powerful technique used in software development to enable or disable specific features or functionality within an application at runtime, without the need for redeployment. This practice is widely used for progressive rollouts, testing in production, A/B testing, and improving CI/CD workflows. Feature flags can also significantly enhance how features are delivered to users.

Feature flags are implemented in the code as conditional statements, allowing developers to control whether a particular feature is active or not, and also to decouple feature release from deployment. By wrapping a feature's code with a flag, you can control the behavior of the application without changing the underlying codebase, which results in greater flexibility and faster iterations.

Feature flags can be implemented in a variety of ways depending on your needs. They can be managed either through hardcoded values, configuration files, or external services such as Azure App Configuration, LaunchDarkly, or even your own backend.

Let's focus on a basic implementation of Feature Flags in a Blazor app. I will use Azure App Service configuration for this blog post.

Step 1. Set Up Azure App Configuration

You will need to create an Azure App Configuration instance and store your feature flags there.

Feature Flag Enabled

Step 2. Add Azure App Configuration Connection String

Retrieve the Connection String from your Azure resource:

Connection String

Add it to appsettings.json file in your Blazor project:

appsettings file

Step 3. Add the NuGet packages

Add the Microsoft.Azure.AppConfiguration.AspNetCore and Microsoft.FeatureManagement.AspNetCore NuGet packages to your project.

NuGet packages

Step 4. Modify Program.cs

Modify Program.cs to add the configuration provider and register the middleware and service:

Image description

Step 5. Use the Feature Flag in your code

For example, let's show/hide the Weather page from the menu:

First, use FeatureManager in your page to get the reference to the Feature Flag:

Image description

Secondly, show content based on the value.

Image description

Step 6. Let's test it.

Run the app and see the Weather option in the menu:

Image description

Now, let's disable the Feature Flag:

Image description

Now, refresh the website. No weather option is displayed this time:

Image description

As you can see, Feature Flags are easy to implement. Feature flags are an essential tool for managing application features in Blazor app. They provide flexibility in how features are rolled out, tested, and managed, enabling a more dynamic, reliable, and scalable way to deliver and manage features in production.

Remember to follow the rest of the interesting publications of the C# Advent Calendar 2024. You can also follow the conversation on Twitter with the hashtag #csadvent.

Thank you for reading. Until next time!
Luis

Top comments (0)