Exemple #1
0
        /// <summary>
        /// Converts the most recently added configuration source into a read-only configuration source.
        /// </summary>
        /// <param name="builder">The configuration builder.</param>
        /// <returns>The configuration builder.</returns>
        /// <remarks>
        /// <para>
        /// This method is intended to be chained after each source that needs to be made read-only.
        /// </para>
        ///
        /// <code>
        /// IConfiguration configuration = new ConfigurationBuilder()
        ///     .AddInMemoryCollection(defaultSettings).AsReadOnly()
        ///     .AddIniFile("usersettings.ini").AsReadOnly()
        ///     .AddSQLite()
        ///     .Build();
        ///
        /// // This will only update the SQLite configuration provider
        /// configuration["Hello"] = "World";
        /// </code>
        /// </remarks>
        /// <seealso cref="ReadOnlyConfigurationSource"/>
        public static IConfigurationBuilder AsReadOnly(this IConfigurationBuilder builder)
        {
            int index = builder.Sources.Count - 1;
            IConfigurationSource source         = builder.Sources[index];
            IConfigurationSource readOnlySource = new ReadOnlyConfigurationSource(source);

            builder.Sources[index] = readOnlySource;
            return(builder);
        }
Exemple #2
0
        /// <summary>
        /// Configures an <see cref="IConfigurationBuilder"/> with read-only configuration sources.
        /// </summary>
        /// <param name="builder">The configuration builder.</param>
        /// <param name="builderAction">The action to set up configuration sources that will be made read-only.</param>
        /// <returns>The configuration builder.</returns>
        /// <remarks>
        /// <para>
        /// This method is intended to encapsulate the builder action that creates a group of read-only providers.
        /// </para>
        ///
        /// <code>
        /// IConfiguration configuration = new ConfigurationBuilder()
        ///     .ConfigureReadOnly(readOnlyBuilder => readOnlyBuilder
        ///         .AddInMemoryCollection(defaultSettings)
        ///         .AddIniFile("usersettings.ini"))
        ///     .AddSQLite()
        ///     .Build();
        ///
        /// // This will only update the SQLite configuration provider
        /// configuration["Hello"] = "World";
        /// </code>
        /// </remarks>
        public static IConfigurationBuilder ConfigureReadOnly(this IConfigurationBuilder builder, Action <IConfigurationBuilder> builderAction)
        {
            ReferenceEqualityComparer referenceEqualityComparer = new ReferenceEqualityComparer();
            HashSet <object>          originalSources           = new HashSet <object>(builder.Sources, referenceEqualityComparer);

            builderAction(builder);

            for (int i = 0; i < builder.Sources.Count; i++)
            {
                IConfigurationSource source = builder.Sources[i];

                if (originalSources.Contains(source))
                {
                    continue;
                }

                IConfigurationSource readOnlySource = new ReadOnlyConfigurationSource(source);
                builder.Sources[i] = readOnlySource;
            }

            return(builder);
        }