How to use Feature Flags in a Blazor Application
Published: 2022-03-15 11:00:01

On Azure exists a service to provide a central manage application settings and features, called Azure App Configuration.

It's a really useful service especially for applications running in the cloud because they are distributed by nature.
With Azure App Configuration you can spread all the configuration trough all the components and services of your application.

In this article I will show you how to use the feature management to enable or disable functionalities in your application without a new deploy or restart the application.

## Create an App Configuration Store on Azure 

To create a new App Configuration, from the [Azure Portal](https://portal.azure.com), click on the search bar on the top and start typing "app configuration"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3hrjlaurw4vksc2ljtg.png)

Click on create and fill the form with your information.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3miagrtavy1rmk90n95.png)

After the deployment, select the new App Configuration from your resource group.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vkkeh1xc9rur0kvedpek.png)

From the left navigation pane, click on "Features Manager" under the section "Operations".

Click on "Add" to create a new Feature.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ucn4b2eqan75w521owy.png)

Enter the Feature flag name "counter" and enable the feature flag.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fhoug7le7d499wi8uulw.png)

Repeat the operation and call the new feature "weather".

## Configure the Blazor Application

For this post, I use a Blazor Server App because, at the moment, **the WASM project is not supported**.

Add the NuGet packages called "Microsoft.FeatureManagement.AspNetCore" and "Microsoft.Azure.AppConfiguration.AspNetCore".

In the file "Program.cs", replace the code at line number 7 with the snippet below.

```csharp

var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    WebRootPath = "wwwroot"
});

builder.WebHost.ConfigureAppConfiguration(config =>
            {
                config.AddAzureAppConfiguration(options =>
                    options.Connect("CONNECTIONSTRING").UseFeatureFlags());
            });
```

Replace the CONNECTIONSTRING with the Azure App Configuration connection string from Azure Portal.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nensdvw3if9lxye8o4pf.png)
  
Create a new razor component on the project root called "FeatureFlagComponent.razor".

Replate the existing code in the file with the code below:

```csharp
@using Microsoft.FeatureManagement
@inject IFeatureManager FeatureManager

@if (featureIsEnabled)
{
    @ChildContent
}

@code {
    private bool featureIsEnabled = false;

    [Parameter] public RenderFragment ChildContent { get; set; }
    [Parameter] public string FlagName { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (string.IsNullOrEmpty(FlagName)) return;
        featureIsEnabled = await FeatureManager.IsEnabledAsync(FlagName);
    }
}
```

Now you are ready to use this new component in your code.
For instance, we want to hide or show a menu entry on the navigation bar.
Open the file NavMenu.razor and replace the two menu entries with this piece of code:

```csharp
        <FeatureFlagComponent FlagName="counter">
            <div class="nav-item px-3">
                <NavLink class="nav-link" href="counter">
                    <span class="oi oi-plus" aria-hidden="true"></span> Counter
                </NavLink>
            </div>
        </FeatureFlagComponent>

        <FeatureFlagComponent FlagName="weather">
            <div class="nav-item px-3">
                <NavLink class="nav-link" href="fetchdata">
                    <span class="oi oi-list-rich" aria-hidden="true"></span> Fetch data
                </NavLink>
            </div>
        </FeatureFlagComponent>

```

Now you can try to enable or disable the features from Azure and see the menu entries that appear or disappear on the navigation menu.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pezjqjw20mb6enp7pfmv.png)