|
| 1 | +# Microsoft.Extensions.Configuration.Binder |
| 2 | + |
| 3 | +Provides the functionality to bind an object to data in configuration providers for [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/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](https://learn.microsoft.com/dotnet/api/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](https://learn.microsoft.com/dotnet/core/extensions/configuration#configuration-providers), for example, [Microsoft.Extensions.Configuration.Json](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Json/) for the JSON provider. |
| 4 | + |
| 5 | +Documentation can be found at https://learn.microsoft.com/dotnet/core/extensions/configuration |
| 6 | + |
| 7 | +## Contribution Bar |
| 8 | +- [x] [We consider new features, new APIs, bug fixes, and performance changes](../../libraries/README.md#primary-bar) |
| 9 | + |
| 10 | +The APIs and functionality are mature, but do get extended occasionally. |
| 11 | + |
| 12 | +## Deployment |
| 13 | +[Microsoft.Extensions.Configuration.Binder](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Binder/) is included in the ASP.NET Core shared framework. The package is deployed as out-of-band (OOB) too and can be referenced into projects directly. |
| 14 | + |
| 15 | +## Example |
| 16 | +The following example shows how to bind a JSON configuration section to .NET objects. |
| 17 | + |
| 18 | +```cs |
| 19 | +using System; |
| 20 | +using Microsoft.Extensions.Configuration; |
| 21 | + |
| 22 | +class Settings |
| 23 | +{ |
| 24 | + public string Server { get; set; } |
| 25 | + public string Database { get; set; } |
| 26 | + public Endpoint[] Endpoints { get; set; } |
| 27 | +} |
| 28 | + |
| 29 | +class Endpoint |
| 30 | +{ |
| 31 | + public string IPAddress { get; set; } |
| 32 | + public int Port { get; set; } |
| 33 | +} |
| 34 | + |
| 35 | +class Program |
| 36 | +{ |
| 37 | + static void Main() |
| 38 | + { |
| 39 | + // Build a configuration object from JSON file |
| 40 | + IConfiguration config = new ConfigurationBuilder() |
| 41 | + .AddJsonFile("appsettings.json") |
| 42 | + .Build(); |
| 43 | + |
| 44 | + // Bind a configuration section to an instance of Settings class |
| 45 | + Settings settings = config.GetSection("Settings").Get<Settings>(); |
| 46 | + |
| 47 | + // Read simple values |
| 48 | + Console.WriteLine($"Server: {settings.Server}"); |
| 49 | + Console.WriteLine($"Database: {settings.Database}"); |
| 50 | + |
| 51 | + // Read nested objects |
| 52 | + Console.WriteLine("Endpoints: "); |
| 53 | + |
| 54 | + foreach (Endpoint endpoint in settings.Endpoints) |
| 55 | + { |
| 56 | + Console.WriteLine($"{endpoint.IPAddress}:{endpoint.Port}"); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +To run this example, include an `appsettings.json` file with the following content in your project: |
| 63 | + |
| 64 | +```json |
| 65 | +{ |
| 66 | + "Settings": { |
| 67 | + "Server": "example.com", |
| 68 | + "Database": "Northwind", |
| 69 | + "Endpoints": [ |
| 70 | + { |
| 71 | + "IPAddress": "192.168.0.1", |
| 72 | + "Port": "80" |
| 73 | + }, |
| 74 | + { |
| 75 | + "IPAddress": "192.168.10.1", |
| 76 | + "Port": "8080" |
| 77 | + } |
| 78 | + ] |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +You can include a configuration file using a code like this in your `.csproj` file: |
| 84 | + |
| 85 | +```xml |
| 86 | +<ItemGroup> |
| 87 | + <Content Include="appsettings.json"> |
| 88 | + <CopyToOutputDirectory>Always</CopyToOutputDirectory> |
| 89 | + </Content> |
| 90 | +</ItemGroup> |
| 91 | +``` |
0 commit comments