src/Kros.AspNetCore/Extensions/ConfigurationBuilderExtenstions.cs
/// <summary>
/// Adds the azure application configuration.
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="environmentName">Environment name.</param>
/// <param name="refreshConfiguration">A callback used to configure Azure App Configuration refresh options.</param>
/// <returns>The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining.</returns>
/// <remarks>
/// Configuration should contain <b>AppConfig</b> section, which is mapped to <see cref="AppConfigOptions "/> class.
/// </remarks>
Adds the azure application configuration.
Adds the azure application configuration .
public static IConfigurationBuilder AddAzureAppConfig(
            this IConfigurationBuilder config,
            string environmentName,
            Action<AzureAppConfigurationRefreshOptions> refreshConfiguration = null)
        {
            IConfigurationRoot settings = config.Build();
            AppConfigOptions appConfig = new();
            settings.Bind("AppConfig", appConfig);
            if (string.IsNullOrWhiteSpace(appConfig.Endpoint))
            {
                return config;
            }
            config.AddAzureAppConfiguration(options =>
            {
                DefaultAzureCredentialOptions credentialOptions = new()
                {
                    ManagedIdentityClientId = appConfig.IdentityClientId
                };
                var credential = new DefaultAzureCredential(credentialOptions);
                options
                    .Connect(new Uri(appConfig.Endpoint), credential)
                    .ConfigureKeyVault(kv => kv.SetCredential(credential));
                ConfigureCacheRefresh(options, appConfig, refreshConfiguration);
                IEnumerable<string> prefixes = appConfig.Settings.Where(prefix => !prefix.IsNullOrWhiteSpace());
                foreach (string prefix in prefixes)
                {
                    options
                        .Select($"{prefix}:*", LabelFilter.Null)
                        .Select($"{prefix}:*", environmentName)
                        .TrimKeyPrefix($"{prefix}:");
                }
                if (appConfig.UseFeatureFlags)
                {
                    options
                        .Select("_", LabelFilter.Null)
                        .Select("_", environmentName)
                        .UseFeatureFlags();
                }
            });
            return config;
        }
public static IConfigurationBuilder AddAzureAppConfig ( this IConfigurationBuilder config , string environmentName , Action < AzureAppConfigurationRefreshOptions > refreshConfiguration = null ) { IConfigurationRoot settings = config . Build ( ) ; AppConfigOptions appConfig = new ( ) ; settings . Bind ( " " , appConfig ) ; if ( string . IsNullOrWhiteSpace ( appConfig . Endpoint ) ) { return config ; } config . AddAzureAppConfiguration ( options => { DefaultAzureCredentialOptions credentialOptions = new ( ) { ManagedIdentityClientId = appConfig . IdentityClientId } ; var credential = new DefaultAzureCredential ( credentialOptions ) ; options . Connect ( new Uri ( appConfig . Endpoint ) , credential ) . ConfigureKeyVault ( kv => kv . SetCredential ( credential ) ) ; ConfigureCacheRefresh ( options , appConfig , refreshConfiguration ) ; IEnumerable < string > prefixes = appConfig . Settings . Where ( prefix => ! prefix . IsNullOrWhiteSpace ( ) ) ; foreach ( string prefix in prefixes ) { options . Select ( $" { prefix } :* " , LabelFilter . Null ) . Select ( $" { prefix } :* " , environmentName ) . TrimKeyPrefix ( $" { prefix } : " ) ; } if ( appConfig . UseFeatureFlags ) { options . Select ( " " , LabelFilter . Null ) . Select ( " " , environmentName ) . UseFeatureFlags ( ) ; } } ) ; return config ; }
Adds the azure application configuration.
Adds the azure application configuration .
The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining.
The same instance of the Microsoft . Extensions . Hosting . IHostBuilder for chaining .
IConfigurationBuilder
this
environmentName
string
None
refreshConfiguration
Action<AzureAppConfigurationRefreshOptions>
A callback used to configure Azure App Configuration refresh options.
A callback used to configure Azure App Configuration refresh options .
config
None
remarks
Configuration should contain AppConfig class.
Configuration should contain AppConfig class .