ConfigurationBuilder Class

Definition

Used to build key/value-based configuration settings for use in an application.

public ref class ConfigurationBuilder : Microsoft::Extensions::Configuration::IConfigurationBuilder
public class ConfigurationBuilder : Microsoft.Extensions.Configuration.IConfigurationBuilder
type ConfigurationBuilder = class
    interface IConfigurationBuilder
Public Class ConfigurationBuilder
Implements IConfigurationBuilder
Inheritance
ConfigurationBuilder
Implements

Remarks

By using this class, you can store configuration values in a JSON file, for example, and then retrieve them at run time. For more information, see Configuration - basic example.

If you're migrating an app to .NET Core 3.1 or a later version, you can use this class to replace the app.config file functionality that you may have previously used to configure your app.

Example

The following code snippet shows a simplified app that creates a ConfigurationBuilder object to retrieve a string value from a JSON file at run time.

using Microsoft.Extensions.Configuration;

IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true);
IConfigurationRoot root = builder.Build();

Console.WriteLine($"Hello, { root["weather"] } world!");

/* This program outputs the following text:
 * 
 * Hello, stormy world!
 */

The contents of the JSON file are shown here.

{
  "weather": "stormy"
}

Constructors

ConfigurationBuilder()

Initializes a new instance of the ConfigurationBuilder class.

Properties

Properties

Gets a key/value collection that can be used to share data between the IConfigurationBuilder and the registered configuration providers.

Sources

Gets the sources used to obtain configuration values.

Methods

Add(IConfigurationSource)

Adds a new configuration source.

Build()

Builds an IConfiguration with keys and values from the set of providers registered in Sources.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Extension Methods

AddApplicationMetadata(IConfigurationBuilder, IHostEnvironment, String)

Registers a configuration provider for application metadata.

AddAzureKeyVault(IConfigurationBuilder, AzureKeyVaultConfigurationOptions)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddAzureKeyVault(IConfigurationBuilder, String)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddAzureKeyVault(IConfigurationBuilder, String, KeyVaultClient, IKeyVaultSecretManager)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddAzureKeyVault(IConfigurationBuilder, String, IKeyVaultSecretManager)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddAzureKeyVault(IConfigurationBuilder, String, String, X509Certificate2)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddAzureKeyVault(IConfigurationBuilder, String, String, X509Certificate2, IKeyVaultSecretManager)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddAzureKeyVault(IConfigurationBuilder, String, String, String)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddAzureKeyVault(IConfigurationBuilder, String, String, String, IKeyVaultSecretManager)

Adds an IConfigurationProvider that reads configuration values from the Azure KeyVault.

AddConfiguration(IConfigurationBuilder, IConfiguration)

Adds an existing configuration to configurationBuilder.

AddConfiguration(IConfigurationBuilder, IConfiguration, Boolean)

Adds an existing configuration to configurationBuilder.

AddCommandLine(IConfigurationBuilder, Action<CommandLineConfigurationSource>)

Adds an IConfigurationProvider that reads configuration values from the command line.

AddCommandLine(IConfigurationBuilder, String[])

Adds a CommandLineConfigurationProvider that reads configuration values from the command line.

AddCommandLine(IConfigurationBuilder, String[], IDictionary<String,String>)

Adds a CommandLineConfigurationProvider that reads configuration values from the command line using the specified switch mappings.

Add<TSource>(IConfigurationBuilder, Action<TSource>)

Adds a new configuration source.

AddUserSecrets(IConfigurationBuilder)

Adds the user secrets configuration source. Searches the assembly from GetEntryAssembly() for an instance of UserSecretsIdAttribute.

AddUserSecrets(IConfigurationBuilder, Assembly)

Adds the user secrets configuration source.

AddUserSecrets(IConfigurationBuilder, String)

Adds the user secrets configuration source with specified secrets ID.

AddUserSecrets<T>(IConfigurationBuilder)

Adds the user secrets configuration source. Searches the assembly that contains type T for an instance of UserSecretsIdAttribute.

AddEnvironmentVariables(IConfigurationBuilder)

Adds an IConfigurationProvider that reads configuration values from environment variables.

AddEnvironmentVariables(IConfigurationBuilder, Action<EnvironmentVariablesConfigurationSource>)

Adds an IConfigurationProvider that reads configuration values from environment variables.

AddEnvironmentVariables(IConfigurationBuilder, String)

Adds an IConfigurationProvider that reads configuration values from environment variables with a specified prefix.

GetFileLoadExceptionHandler(IConfigurationBuilder)

Gets the default IFileProvider to be used for file-based providers.

GetFileProvider(IConfigurationBuilder)

Gets the default IFileProvider to be used for file-based providers.

SetBasePath(IConfigurationBuilder, String)

Sets the FileProvider for file-based providers to a PhysicalFileProvider with the base path.

SetFileLoadExceptionHandler(IConfigurationBuilder, Action<FileLoadExceptionContext>)

Sets a default action to be invoked for file-based providers when an error occurs.

SetFileProvider(IConfigurationBuilder, IFileProvider)

Sets the default IFileProvider to be used for file-based providers.

AddIniFile(IConfigurationBuilder, IFileProvider, String, Boolean, Boolean)

Adds a INI configuration source to builder.

AddIniFile(IConfigurationBuilder, Action<IniConfigurationSource>)

Adds a INI configuration source to builder.

AddIniFile(IConfigurationBuilder, String)

Adds the INI configuration provider at path to builder.

AddIniFile(IConfigurationBuilder, String, Boolean)

Adds the INI configuration provider at path to builder.

AddIniFile(IConfigurationBuilder, String, Boolean, Boolean)

Adds the INI configuration provider at path to builder.

AddIniStream(IConfigurationBuilder, Stream)

Adds a INI configuration source to builder.

AddJsonFile(IConfigurationBuilder, IFileProvider, String, Boolean, Boolean)

Adds a JSON configuration source to builder.

AddJsonFile(IConfigurationBuilder, Action<JsonConfigurationSource>)

Adds a JSON configuration source to builder.

AddJsonFile(IConfigurationBuilder, String)

Adds the JSON configuration provider at path to builder.

AddJsonFile(IConfigurationBuilder, String, Boolean)

Adds the JSON configuration provider at path to builder.

AddJsonFile(IConfigurationBuilder, String, Boolean, Boolean)

Adds the JSON configuration provider at path to builder.

AddJsonStream(IConfigurationBuilder, Stream)

Adds a JSON configuration source to builder.

AddKeyPerFile(IConfigurationBuilder, Action<KeyPerFileConfigurationSource>)

Adds configuration using files from a directory. File names are used as the key, file contents are used as the value.

AddKeyPerFile(IConfigurationBuilder, String)

Adds configuration using files from a directory. File names are used as the key, file contents are used as the value.

AddKeyPerFile(IConfigurationBuilder, String, Boolean)

Adds configuration using files from a directory. File names are used as the key, file contents are used as the value.

AddKeyPerFile(IConfigurationBuilder, String, Boolean, Boolean)

Adds configuration using files from a directory. File names are used as the key, file contents are used as the value.

AddInMemoryCollection(IConfigurationBuilder)

Adds the memory configuration provider to configurationBuilder.

AddInMemoryCollection(IConfigurationBuilder, IEnumerable<KeyValuePair<String,String>>)

Adds the memory configuration provider to configurationBuilder.

AddNewtonsoftJsonFile(IConfigurationBuilder, IFileProvider, String, Boolean, Boolean)

Adds a JSON configuration source to builder.

AddNewtonsoftJsonFile(IConfigurationBuilder, Action<NewtonsoftJsonConfigurationSource>)

Adds a JSON configuration source to builder.

AddNewtonsoftJsonFile(IConfigurationBuilder, String)

Adds the JSON configuration provider at path to builder.

AddNewtonsoftJsonFile(IConfigurationBuilder, String, Boolean)

Adds the JSON configuration provider at path to builder.

AddNewtonsoftJsonFile(IConfigurationBuilder, String, Boolean, Boolean)

Adds the JSON configuration provider at path to builder.

AddNewtonsoftJsonStream(IConfigurationBuilder, Stream)

Adds a Newtonsoft JSON configuration source to builder.

AddUserSecrets(IConfigurationBuilder, Assembly)

Adds the user secrets configuration source. This searches assembly for an instance of UserSecretsIdAttribute, which specifies a user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddUserSecrets(IConfigurationBuilder, Assembly, Boolean)

Adds the user secrets configuration source. This searches assembly for an instance of UserSecretsIdAttribute, which specifies a user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddUserSecrets(IConfigurationBuilder, Assembly, Boolean, Boolean)

Adds the user secrets configuration source. This searches assembly for an instance of UserSecretsIdAttribute, which specifies a user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddUserSecrets(IConfigurationBuilder, String)

Adds the user secrets configuration source with specified user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddUserSecrets(IConfigurationBuilder, String, Boolean)

Adds the user secrets configuration source with specified user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddUserSecrets<T>(IConfigurationBuilder)

Adds the user secrets configuration source. Searches the assembly that contains type T for an instance of UserSecretsIdAttribute, which specifies a user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddUserSecrets<T>(IConfigurationBuilder, Boolean)

Adds the user secrets configuration source. Searches the assembly that contains type T for an instance of UserSecretsIdAttribute, which specifies a user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddUserSecrets<T>(IConfigurationBuilder, Boolean, Boolean)

Adds the user secrets configuration source. Searches the assembly that contains type T for an instance of UserSecretsIdAttribute, which specifies a user secrets ID.

A user secrets ID is unique value used to store and identify a collection of secret configuration values.

AddXmlFile(IConfigurationBuilder, IFileProvider, String, Boolean, Boolean)

Adds a XML configuration source to builder.

AddXmlFile(IConfigurationBuilder, Action<XmlConfigurationSource>)

Adds a XML configuration source to builder.

AddXmlFile(IConfigurationBuilder, String)

Adds the XML configuration provider at path to builder.

AddXmlFile(IConfigurationBuilder, String, Boolean)

Adds the XML configuration provider at path to builder.

AddXmlFile(IConfigurationBuilder, String, Boolean, Boolean)

Adds the XML configuration provider at path to builder.

AddXmlStream(IConfigurationBuilder, Stream)

Adds a XML configuration source to builder.

Applies to

See also