-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add package readme for Microsoft.Extensions.Configuration.Binder #77649
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/libraries/Microsoft.Extensions.Configuration.Binder/src/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# Microsoft.Extensions.Configuration.Binder | ||
|
||
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. | ||
|
||
Documentation can be found at https://learn.microsoft.com/dotnet/core/extensions/configuration | ||
|
||
## Contribution Bar | ||
- [x] [We consider new features, new APIs, bug fixes, and performance changes](../../../libraries/README.md#primary-bar) | ||
|
||
The APIs and functionality are mature, but do get extended occasionally. | ||
|
||
## Deployment | ||
[Microsoft.Extensions.Configuration.Binder](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.Binder/) is not included in the shared framework. The package is deployed as out-of-band (OOB) and needs to be installed into projects directly. | ||
|
||
## Example | ||
The following example shows how to bind a JSON configuration section to .NET objects. | ||
|
||
```cs | ||
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: | ||
|
||
```json | ||
{ | ||
"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: | ||
|
||
```xml | ||
<ItemGroup> | ||
<Content Include="appsettings.json"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change this to be
is included in the ASP.NET core shared framework
.We'll need to fix this in the rest of readmes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you meant "is included..."? It's actually there looking at shipped binaries. But it is not included in regular Netcoreapp shared framework. So shouldn't it be something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I have suggested some tweak in my comment #77649 (review). Let me double check if this library deployment in aspnet core. Thanks for the catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, didn't noticed the latest review suggestion when commenting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I confirmed this library is part of ASP.NET Core Runtime