Microsoft.Extensions.Configuration.Binder 9.0.0-rc.2.24473.5
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.ConfigurationBinder
Microsoft.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.Hosting
Hosting and startup infrastructures for applications.
|
134 |
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
|
26 |
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
|
5,403 |
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
|
8,834 |
Microsoft.Extensions.Logging.Configuration
Configuration support for Microsoft.Extensions.Logging.
|
17 |
Microsoft.Extensions.Logging.Configuration
Configuration support for Microsoft.Extensions.Logging.
|
23 |
Microsoft.Extensions.Logging.Configuration
Configuration support for Microsoft.Extensions.Logging.
|
25 |
Microsoft.Extensions.Logging.Configuration
Configuration support for Microsoft.Extensions.Logging.
|
151 |
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
17 |
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
21 |
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
130 |
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
254 |
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
5,445 |
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
|
8,918 |
Quantum.Framework.AspNetCore
Package Description
|
26 |
Quantum.Framework.AspNetCore
保存claims数据到IScmSessionBug修复
|
17 |
Quantum.Framework.AspNetCore
增加redis hash方法及验证token有效性
|
23 |
Quantum.Framework.AspNetCore
增加用户类型及API结果类型
|
22 |
Quantum.Framework.AspNetCore
增加获取用户授权KEY及获取用户角色方法
|
35 |
Serilog.Settings.Configuration
Microsoft.Extensions.Configuration (appsettings.json) support for Serilog.
|
119 |
.NET Framework 4.6.2
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-rc.2.24473.5)
.NET 8.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-rc.2.24473.5)
.NET 9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-rc.2.24473.5)
.NET Standard 2.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.0-rc.2.24473.5)