Remote Configuration
The Remote Configuration provider allows you to fetch configuration from an HTTP endpoint and cache it locally. This is useful for feature flags, A/B testing, or any scenario where you need server-driven configuration.
Setup (MAUI)
Section titled “Setup (MAUI)”The easiest way to add remote configuration in a MAUI app is with the MauiAppBuilder extension:
builder.AddRemoteConfigurationMaui("https://myserver.com/config");This will:
- Fetch configuration from the specified URI
- Cache it locally as
remotesettings.jsonin the app’s data directory - Register
IRemoteConfigurationProviderin the DI container
Custom File Name
Section titled “Custom File Name”You can specify a custom local cache file name:
builder.AddRemoteConfigurationMaui( "https://myserver.com/config", configurationFileName: "myconfig.json");Custom Data Retrieval
Section titled “Custom Data Retrieval”If your server requires custom headers, authentication, or returns a non-standard format, you can supply a custom data retrieval function:
builder.AddRemoteConfigurationMaui( "https://myserver.com/config", getData: async (remoteConfig, cancellationToken) => { using var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("X-Api-Key", "my-key"); var response = await httpClient.GetStringAsync(remoteConfig.Uri, cancellationToken); return response; });Setup (IConfigurationBuilder)
Section titled “Setup (IConfigurationBuilder)”You can also add remote configuration directly to an IConfigurationBuilder:
builder.Configuration.AddRemoteMaui( "https://myserver.com/config", configurationFileName: "remotesettings.json");IRemoteConfigurationProvider
Section titled “IRemoteConfigurationProvider”When registered via the MAUI extension, IRemoteConfigurationProvider is available in the DI container. It extends IConfigurationProvider and adds:
public interface IRemoteConfigurationProvider : IConfigurationProvider{ DateTimeOffset? LastLoaded { get; } Task LoadAsync(CancellationToken cancellationToken = default);}Refreshing Configuration
Section titled “Refreshing Configuration”You can manually refresh the remote configuration at any time:
public class MyService{ readonly IRemoteConfigurationProvider remoteConfig;
public MyService(IRemoteConfigurationProvider remoteConfig) { this.remoteConfig = remoteConfig; }
public async Task RefreshConfig() { await remoteConfig.LoadAsync(); // Configuration is now updated // IOptionsMonitor<T> will fire change notifications }}Reactive Updates
Section titled “Reactive Updates”Combine remote configuration with IOptionsMonitor<T> to react to configuration changes:
services.Configure<FeatureFlags>(config.GetSection("FeatureFlags"));
public class MyViewModel{ public MyViewModel( IOptionsMonitor<FeatureFlags> options, IRemoteConfigurationProvider remoteConfig) { options.OnChange(flags => { // React to remote config changes }); }}