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.
Step 2. Add Azure App Configuration Connection String
Retrieve the Connection String from your Azure resource:
Add it to appsettings.json
file in your Blazor project:
Step 3. Add the NuGet packages
Add the Microsoft.Azure.AppConfiguration.AspNetCore
and Microsoft.FeatureManagement.AspNetCore
NuGet packages to your project.
Step 4. Modify Program.cs
Modify Program.cs
to add the configuration provider and register the middleware and service:
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:
Secondly, show content based on the value.
Step 6. Let's test it.
Run the app and see the Weather option in the menu:
Now, let's disable the Feature Flag:
Now, refresh the website. No weather option is displayed this time:
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)