Microsoft.Extensions.Configuration.Binder 9.0.0-preview.4.24266.19
About
Provides the functionality to bind an object to data in configuration providers for Microsoft.Extensions.Configuration. This package enables you to represent the configuration data as strongly-typed classes defined in the application code. To bind a configuration, use the Microsoft.Extensions.Configuration.ConfigurationBinder.Get extension method on the IConfiguration object. To use this package, you also need to install a package for the configuration provider, for example, Microsoft.Extensions.Configuration.Json for the JSON provider.
The types contained in this assembly use Reflection at runtime which is not friendly with linking or AOT. To better support linking and AOT as well as provide more efficient strongly-typed binding methods - this package also provides a source generator. This generator is enabled by default when a project sets PublishAot but can also be enabled using <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>.
Key Features
- Configuring existing type instances from a configuration section (Bind)
- Constructing new configured type instances from a configuration section (Get & GetValue)
- Generating source to bind objects from a configuration section without a runtime reflection dependency.
How to Use
The following example shows how to bind a JSON configuration section to .NET objects.
using System;
using Microsoft.Extensions.Configuration;
class Settings
{
public string Server { get; set; }
public string Database { get; set; }
public Endpoint[] Endpoints { get; set; }
}
class Endpoint
{
public string IPAddress { get; set; }
public int Port { get; set; }
}
class Program
{
static void Main()
{
// Build a configuration object from JSON file
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
// Bind a configuration section to an instance of Settings class
Settings settings = config.GetSection("Settings").Get<Settings>();
// Read simple values
Console.WriteLine($"Server: {settings.Server}");
Console.WriteLine($"Database: {settings.Database}");
// Read nested objects
Console.WriteLine("Endpoints: ");
foreach (Endpoint endpoint in settings.Endpoints)
{
Console.WriteLine($"{endpoint.IPAddress}:{endpoint.Port}");
}
}
}
To run this example, include an appsettings.json file with the following content in your project:
{
"Settings": {
"Server": "example.com",
"Database": "Northwind",
"Endpoints": [
{
"IPAddress": "192.168.0.1",
"Port": "80"
},
{
"IPAddress": "192.168.10.1",
"Port": "8080"
}
]
}
}
You can include a configuration file using a code like this in your .csproj file:
<ItemGroup>
<Content Include="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
You can add the following property to enable the source generator. This requires a .NET 8.0 SDK or later.
<PropertyGroup>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
</PropertyGroup>
Main Types
The main types provided by this library are:
Microsoft.Extensions.Configuration.ConfigurationBinderMicrosoft.Extensions.Configuration.BinderOptions
Additional Documentation
Related Packages
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.Abstractions
- Microsoft.Extensions.Configuration.CommandLine
- Microsoft.Extensions.Configuration.EnvironmentVariables
- Microsoft.Extensions.Configuration.FileExtensions
- Microsoft.Extensions.Configuration.Ini
- Microsoft.Extensions.Configuration.Json
- Microsoft.Extensions.Configuration.UserSecrets
- Microsoft.Extensions.Configuration.Xml
Feedback & Contributing
Microsoft.Extensions.Configuration.Binder is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.
Showing the top 20 packages that depend on Microsoft.Extensions.Configuration.Binder.
| Packages | Downloads |
|---|---|
|
Microsoft.Extensions.ServiceDiscovery.Abstractions
Provides abstractions for service discovery. Interfaces defined in this package are implemented in Microsoft.Extensions.ServiceDiscovery and other service discovery packages.
|
9 |
|
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
8 |
|
Microsoft.AspNetCore.Components.WebAssembly
Build client-side single-page applications (SPAs) with Blazor running under WebAssembly.
This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/379bfc7b2559e7cc9f42f997a497b2f2dd8e12d2
|
8 |
|
Aspire.Hosting
Core abstractions for the .NET Aspire application model.
|
8 |
|
Aspire.StackExchange.Redis.OutputCaching
A Redis® implementation for ASP.NET Core Output Caching that integrates with Aspire, including health checks, logging, and telemetry.
|
7 |
|
Microsoft.Extensions.ServiceDiscovery
Provides extensions to HttpClient that enable service discovery based on configuration.
|
7 |
|
Microsoft.FeatureManagement
Microsoft.FeatureManagement provides a way to develop and expose application functionality based on features. Many applications have special requirements when a new feature is developed such as when the feature should be enabled and under what conditions. This library provides a way to define these relationships, and also integrates into common .NET code patterns to make exposing these features possible.
|
7 |
|
Microsoft.Extensions.Http.Resilience
Resilience mechanisms for HttpClient.
|
6 |
|
Aspire.StackExchange.Redis
A generic Redis® client that integrates with Aspire, including health checks, logging, and telemetry.
|
6 |
|
Microsoft.AspNetCore.Components.WebAssembly
Build client-side single-page applications (SPAs) with Blazor running under WebAssembly.
This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/f6b3a5da75eb405046889a5447ec9b14cc29d285
|
6 |
|
Microsoft.Orleans.Core
Core library of Microsoft Orleans used both on the client and server.
|
6 |
|
Microsoft.FeatureManagement
Microsoft.FeatureManagement provides a way to develop and expose application functionality based on features. Many applications have special requirements when a new feature is developed such as when the feature should be enabled and under what conditions. This library provides a way to define these relationships, and also integrates into common .NET code patterns to make exposing these features possible.
|
5 |
|
Microsoft.Extensions.Hosting
Hosting and startup infrastructures for applications.
|
5 |
|
Aspire.Hosting.AppHost
Core library and MSBuild logic for .NET Aspire AppHost projects.
|
5 |
|
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
|
5 |
|
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
5 |
|
Microsoft.Garnet
Garnet is a remote cache-store from Microsoft Research, that offers strong performance (throughput and latency),
scalability, storage, recovery, cluster sharding, key migration, and replication features. Garnet uses the Redis RESP wire
protocol and can work with existing Redis clients.
|
5 |
|
OpenTelemetry.Exporter.OpenTelemetryProtocol
OpenTelemetry protocol exporter for OpenTelemetry .NET
|
5 |
.NET Framework 4.6.2
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-preview.4.24266.19)
.NET 8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-preview.4.24266.19)
.NET 9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-preview.4.24266.19)
.NET Standard 2.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-preview.4.24266.19)