Skip to content

Commit 5c61ad4

Browse files
Add package readme for Microsoft.Extensions.Configuration.Binder (#77649)
1 parent e6700ea commit 5c61ad4

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
```

src/libraries/Microsoft.Extensions.Configuration.Binder/src/Microsoft.Extensions.Configuration.Binder.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<IsPackable>true</IsPackable>
77
<EnableAOTAnalyzer>true</EnableAOTAnalyzer>
8-
<PackageDescription>Functionality to bind an object to data in configuration providers for Microsoft.Extensions.Configuration.</PackageDescription>
8+
<PackageDescription>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.</PackageDescription>
9+
<PackageReadmeFile>README.md</PackageReadmeFile>
910
</PropertyGroup>
1011

1112
<ItemGroup>
1213
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Abstractions\src\Microsoft.Extensions.Configuration.Abstractions.csproj" />
1314
<Compile Include="$(CommonPath)Extensions\ParameterDefaultValue\ParameterDefaultValue.cs" Link="Common\src\Extensions\ParameterDefaultValue\ParameterDefaultValue.cs" />
1415
<Compile Include="$(CommonPath)System\ThrowHelper.cs" Link="Common\System\ThrowHelper.cs" />
16+
<None Include="..\README.md" Pack="true" PackagePath="\"/>
1517
</ItemGroup>
1618

1719
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' != '.NETCoreApp'">

0 commit comments

Comments
 (0)