Microsoft.Extensions.Configuration.Binder 8.0.1

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

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.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
440
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
434
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
434
Microsoft.Extensions.Logging
Logging infrastructure default implementation for Microsoft.Extensions.Logging.
390
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
342
App.Metrics.Extensions.Configuration
Microsoft.Extensions.Configuration support for App Metrics e.g. appsettings.json
107
Com.Ctrip.Framework.Apollo.Configuration
携程Apollo客户端   与Microsoft.Extensions.Configuration集成请使用Com.Ctrip.Framework.Apollo.Configuration包
101
App.Metrics.Health.Extensions.Configuration
Microsoft.Extensions.Configuration support for App Metrics Health e.g. appsettings.json
99
CH.DbConnectionString
Package Description
97
Microsoft.Extensions.Options.ConfigurationExtensions
Provides additional configuration specific functionality related to Options.
84
Serilog.Settings.Configuration
Microsoft.Extensions.Configuration (appsettings.json) support for Serilog.
75
Com.Ctrip.Framework.Apollo.Configuration
携程Apollo客户端   与Microsoft.Extensions.Configuration集成请使用Com.Ctrip.Framework.Apollo.Configuration包
42
CH.DbConnectionString
Package Description
42
Com.Ctrip.Framework.Apollo.Configuration
携程Apollo客户端   与Microsoft.Extensions.Configuration集成请使用Com.Ctrip.Framework.Apollo.Configuration包
36
CH.DbConnectionString
Package Description
36
Com.Ctrip.Framework.Apollo.Configuration
携程Apollo客户端   与Microsoft.Extensions.Configuration集成请使用Com.Ctrip.Framework.Apollo.Configuration包
31
CH.DbConnectionString
Package Description
31
Com.Ctrip.Framework.Apollo.Configuration
携程Apollo客户端   与Microsoft.Extensions.Configuration集成请使用Com.Ctrip.Framework.Apollo.Configuration包
28
CH.DbConnectionString
Package Description
28
Com.Ctrip.Framework.Apollo.Configuration
携程Apollo客户端   与Microsoft.Extensions.Configuration集成请使用Com.Ctrip.Framework.Apollo.Configuration包
19

https://go.microsoft.com/fwlink/?LinkID=799421

Version Downloads Last updated
8.0.1 80 06/03/2024
8.0.0 353 05/31/2024
3.1.0 395 05/31/2024
2.2.0 186 06/03/2024
2.1.1 453 05/31/2024
2.1.0 426 05/31/2024