Dynamic configuration in .NET MAUI application

Dynamic configuration in .NET MAUI application

02 February 2023

.NET MAUI/Xamarin

Buy Me A Coffee

Feature flags and dynamic configuration are essential tools for modern software development. They enable rapid delivery of new features and bug fixes while avoiding the costly write-off of mismatched expectations and accidentally “shipping the wrong thing”. One of the services allowing such capabilities is ConfigCat.

Integrating .NET MAUI with ConfigCat is a great way to make your .NET application more efficient and performant.

ConfigCat is an online service that allows developers to quickly configure their applications with feature flags and variables. It can also be used to dynamically change configuration values based on user preferences and other conditions. Integrating with ConfigCat allows developers to quickly update their applications without having to deploy a new version of the app. This helps to increase the speed of development, while also reducing the risk associated with a new feature launch.

In order to integrate .NET MAUI with ConfigCat, developers need to install the ConfigCat.Client using the NuGet.

To use ConfigCat, developers must first register an instance of the ConfigCatClient:

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder.UseMauiApp<App>();
    builder.Services.AddSingleton(s => ConfigCatClient.Get("YOUR_KEY",
                                                    options =>
                                                    {
                                                        options.PollingMode = PollingModes.AutoPoll(pollInterval: TimeSpan.FromSeconds(90));
                                                        options.Logger.LogLevel = ConfigCat.Client.LogLevel.Info;
                                                    }));
    return builder.Build();
}

It is recommended to have a signle instance of ConfigCatClient. You may setup different PollingMode like LazyLoad or Manual depends on your needs.

After the initialization, let's retrieve values from ConfigCat. For example, if you have a feature flag called beta, you can retrieve its value like this:

Image = this.configCatClient.GetValue("beta", false) ? "botbeta.png" : "bot.png";

This will return a Boolean value indicating whether the feature flag is enabled or disabled.

In addition to feature flags, ConfigCat also allows developers to manage their application’s settings. This helps to keep settings synchronized between test and production environments and make it easy to update settings across the board. With ConfigCat, developers can also create staged rollouts, which allows them to gradually deploy feature updates to a limited number of users for testing and then gradually increase the number of users getting the release. This helps to ensure that any issues are caught before the feature is released to the public, ensuring a smoother launch.

ConfigCat also allows to apply value to specific user. To do that, developer needs to specify the third parameter of the GetValue method:

Title = this.configCatClient.GetValue("beta_gmailusers_mainpagetitle", "Main Page", new User("USER_UNIQUE_IDENTIFIER")
{
    Email = "email@gmail.com"
});

Developers can also subscribe on config changes to dynamically apply updates without restarting the application:

using ConfigCat.Client;
...
public class MainViewModel
{
	private readonly IConfigCatClient configCatClient;
    public MainViewModel(IConfigCatClient configCatClient)
    {
        this.configCatClient = configCatClient;
        configCatClient.ConfigChanged += (_, _) =>
        {
            Initialize();
        };
        Initialize();
    }

    public void Initialize()
    {
        Title = this.configCatClient.GetValue("beta_gmailusers_mainpagetitle", "Main Page", new User("CURRENT_USER_EMAIL")
        {
            Email = "CURRENT_USER_EMAIL"
        });
        Image = this.configCatClient.GetValue("beta", false) ? "botbeta.png" : "bot.png";
    }
}

The full code can be found on GitHub.

Integrating ConfigCat with .NET MAUI is an easy and efficient way to ensure that your application is always optimized and up-to-date. It can help to improve development times, reduce risk associated with feature releases, and provide comprehensive metrics and reporting that can help identify any potential issues. If you’re looking for an easy way to ensure the success of your application, integrating ConfigCat with .NET MAUI is definitely the way to go.

Buy Me A Coffee

Related:

Drag and Drop any content to a .NET MAUI application

The article demonstrates how to add drop functionality to easily move content from the operating system to the .NET MAUI application.

Creating a card stack layout using .NET MAUI

The article describes how to create a custom control for stacking cards.

An unhandled error has occurred. Reload

🗙