ConfigurationBuilder

We found 10 examples in language CSharp for this search. You will see 23 fragments of code.

        public static IHomeAssistantMqttConfigurationBuilder AddLight(
            this IHomeAssistantMqttConfigurationBuilder configurationBuilder,
            ILightMqttEntityConfiguration mqttEntityConfiguration)
        {
            configurationBuilder.TryRegisterEntityBindingManager<ILightMqttEntityConfiguration, ILight, ILightEntityDefinition>();

            configurationBuilder.ServiceCollection.AddSingleton(mqttEntityConfiguration);
            return configurationBuilder;
        }

        public static IHomeAssistantMqttConfigurationBuilder AddLock(
            this IHomeAssistantMqttConfigurationBuilder configurationBuilder,
            ILockMqttEntityConfiguration mqttEntityConfiguration)
        {
            configurationBuilder.TryRegisterEntityBindingManager<ILockMqttEntityConfiguration, ILock, ILockEntityDefinition>();

            configurationBuilder.ServiceCollection.AddSingleton(mqttEntityConfiguration);
            return configurationBuilder;
        }


        private static void AddLocalAppSettings(HostBuilderContext context, IConfigurationBuilder configurationBuilder)
        {
            if (!context.HostingEnvironment.IsDevelopment())
            {
                return;
            }

            Console.WriteLine($"Loading extra config appsettings.Local.json");
            configurationBuilder.AddJsonFile("appsettings.Local.json", true);
        }

        private static void AddEnvironmentVariables(IConfigurationBuilder configurationBuilder)
        {
            configurationBuilder.AddEnvironmentVariables();
        }

        private static IConfigurationBuilder AddPrebuiltConfiguration(this IConfigurationBuilder configurationBuilder, Action<IConfigurationBuilder> prebuiltBuilderAction)
        {
            var prebuiltBuilder = new ConfigurationBuilder();
            prebuiltBuilderAction(prebuiltBuilder);
            return configurationBuilder.AddConfiguration(prebuiltBuilder.Build());
        }


        [Test]
        public void SetValueThrowsExceptionNoSourceRegistered()
        {
            // Arrange
            var configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();

            var expectedMsg = Resources.Error_NoSources;

            // Act
            var ex = Assert.Throws<InvalidOperationException>(() => config["Title"] = "Welcome");

            // Assert
            Assert.AreEqual(expectedMsg, ex.Message);
        }

        [Test]
        public void SameReloadTokenIsReturnedRepeatedly()
        {
            // Arrange
            var configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();

            // Act
            var token1 = config.GetReloadToken();
            var token2 = config.GetReloadToken();

            // Assert
            token1.Should().Be(token2);
        }


        [Fact]
        public void SameReloadTokenIsReturnedRepeatedly()
        {
            // Arrange
            var configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();

            // Act
            var token1 = config.GetReloadToken();
            var token2 = config.GetReloadToken();

            // Assert
            Assert.Same(token1, token2);
        }

        public static IConfigurationRoot BuilderOnly()
        {
            var configurationBuilder = new ConfigurationBuilder();

            // ... add providers

            var configuration = configurationBuilder.Build();

            return configuration;
        }

        public static IConfigurationRoot FileIni()
        {
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);

            configurationBuilder.AddIniFile("appsettings.ini", false, true);

            var configuration = configurationBuilder.Build();

            return configuration;
        }

        public static IConfigurationRoot FileJson()
        {
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);

            configurationBuilder.AddJsonFile("appsettings.json", false, true);

            var configuration = configurationBuilder.Build();

            return configuration;
        }

        public static IConfigurationRoot FileXml()
        {
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.SetBasePath(AppDomain.CurrentDomain.BaseDirectory);

            configurationBuilder.AddXmlFile("appsettings.xml", false, true);

            var configuration = configurationBuilder.Build();

            return configuration;
        }

        public static IConfigurationRoot UserSecrets()
        {
            // to initialise your local store with values
            // type .\appsettings.json | dotnet user-secrets set

            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddUserSecrets<Program>();

            var configuration = configurationBuilder.Build();

            return configuration;
        }


        [Fact]
        public void EmptyStringIsNullable()
        {
            var dic = new Dictionary<string, string>
            {
                {"empty", ""},
            };
            var configurationBuilder = new ConfigurationBuilder();
            configurationBuilder.AddInMemoryCollection(dic);
            var config = configurationBuilder.Build();

            Assert.Null(config.GetValue<bool?>("empty"));
            Assert.Null(config.GetValue<int?>("empty"));
        }

        [Fact]
        public void BinderIgnoresIndexerProperties()
        {
            var configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();
            config.Bind(new List<string>());
        }


        [Fact]
        public void SetValueThrowsExceptionNoSourceRegistered()
        {
            // Arrange
            var configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();

            var expectedMsg = Resources.Error_NoSources;

            // Act
            var ex = Assert.Throws<InvalidOperationException>(() => config["Title"] = "Welcome");

            // Assert
            Assert.Equal(expectedMsg, ex.Message);
        }

        [Fact]
        public void SameReloadTokenIsReturnedRepeatedly()
        {
            // Arrange
            var configurationBuilder = new ConfigurationBuilder();
            var config = configurationBuilder.Build();

            // Act
            var token1 = config.GetReloadToken();
            var token2 = config.GetReloadToken();

            // Assert
            Assert.Same(token1, token2);
        }


        private void AddCommandLine(IConfigurationBuilder configurationBuilder)
        {
            configurationBuilder.AddCommandLine(_args);
        }

        /// <summary>
        /// Load environment variables
        /// </summary>
        /// <param name="configurationBuilder"></param>
        private static void AddEnvironmentVariables(IConfigurationBuilder configurationBuilder)
        {
            configurationBuilder.AddEnvironmentVariables();
        }
        
        /// <summary>
        /// Load non-optional default settings from current working directory
        /// </summary>
        /// <param name="configurationBuilder"></param>
        private void AddDefaults(IConfigurationBuilder configurationBuilder)
        {
            configurationBuilder.AddIniFile("settings.ini");
        }

		/// <summary>
		/// Adds the JSON configuration provider at <paramref name="path"/> to <paramref name="configurationBuilder"/>.
		/// </summary>
		/// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
		/// <param name="configObject">The object of the Json Configuration.</param>
		/// <param name="optional">Determines if loading the configuration provider is optional.</param>
		/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
		/// <exception cref="ArgumentException">If <paramref name="configObject"/> is null or empty.</exception>
		public static IConfigurationBuilder AddJsonObject(
			this IConfigurationBuilder configurationBuilder,
			object configObject,
			bool optional = false)
		{
			if (configurationBuilder == null)
				throw new ArgumentNullException(nameof(configurationBuilder));

			if (configObject == null)
				throw new ArgumentNullException(nameof(configurationBuilder));

			configurationBuilder.Add(new JsonConfigurationSource { JsonObject = configObject });

			return configurationBuilder;
		}

        /// <summary>
        /// Adds the memory configuration provider to <paramref name="configurationBuilder"/>.
        /// </summary>
        /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddInMemoryCollection(this IConfigurationBuilder configurationBuilder)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            configurationBuilder.Add(new MemoryConfigurationSource());
            return configurationBuilder;
        }

        /// <summary>
        /// Adds the memory configuration provider to <paramref name="configurationBuilder"/>.
        /// </summary>
        /// <param name="configurationBuilder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <param name="initialData">The data to add to memory configuration provider.</param>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddInMemoryCollection(
            this IConfigurationBuilder configurationBuilder,
            IEnumerable<KeyValuePair<string, string>> initialData)
        {
            if (configurationBuilder == null)
            {
                throw new ArgumentNullException(nameof(configurationBuilder));
            }

            configurationBuilder.Add(new MemoryConfigurationSource { InitialData = initialData });
            return configurationBuilder;
        }

FlexibleConfiguration.ConfigurationBuilder : IConfigurationBuilder

Constructors :

public ConfigurationBuilder()

Methods :

public IEnumerable<IConfigurationProvider> get_Providers()
public IConfigurationBuilder Add(IConfigurationProvider provider = )
public IConfigurationRoot Build()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()