You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/extensions/configuration.md
+60-1Lines changed: 60 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,9 +24,15 @@ Configuration in .NET is performed using one or more [configuration providers](#
24
24
> [!NOTE]
25
25
> For information about configuring the .NET runtime itself, see [.NET Runtime configuration settings](../runtime-config/index.md).
26
26
27
+
## Concepts and abstractions
28
+
29
+
Given one or more configuration sources, the <xref:Microsoft.Extensions.Configuration.IConfiguration> type provides a unified view of the configuration data. Configuration is read-only, and the configuration pattern is not designed to be programmatically writable. The `IConfiguration` interface is a single representation of all the configuration sources, as shown in the following diagram:
30
+
31
+
:::image type="content" source="media/configuration-sources.svg" lightbox="media/configuration-sources.svg" alt-text="The `IConfiguration` interface is a single representation of all the configuration sources.":::
32
+
27
33
## Configure console apps
28
34
29
-
New .NET console applications created using [dotnet new](../tools/dotnet-new.md) or Visual Studio by default *do not* expose configuration capabilities. To add configuration in a new .NET console application, [add a package reference](../tools/dotnet-add-package.md) to [`Microsoft.Extensions.Hosting`](https://www.nuget.org/packages/Microsoft.Extensions.Hosting). Modify the *Program.cs* file to match the following code:
35
+
.NET console applications created using the [dotnet new](../tools/dotnet-new.md)command template or Visual Studio by default *do not* expose configuration capabilities. To add configuration in a new .NET console application, [add a package reference](../tools/dotnet-add-package.md) to [Microsoft.Extensions.Hosting](https://www.nuget.org/packages/Microsoft.Extensions.Hosting). Modify the *Program.cs* file to match the following code:
@@ -51,6 +57,44 @@ One of the key advantages of using the .NET configuration abstractions is the ab
51
57
52
58
These abstractions are agnostic to their underlying configuration provider (<xref:Microsoft.Extensions.Configuration.IConfigurationProvider>). In other words, you can use an `IConfiguration` instance to access any configuration value from multiple providers.
53
59
60
+
The binder can use different approaches to process configuration values:
61
+
62
+
- Direct deserialization (using built-in converters) for primitive types.
63
+
- The <xref:System.ComponentModel.TypeConverter> for a complex type when the type has one.
64
+
- Reflection for a complex type that has properties.
65
+
66
+
> [!NOTE]
67
+
> The binder has a few limitations:
68
+
>
69
+
> - Properties are ignored if they have private setters or their type can't be converted.
70
+
> - Properties without corresponding configuration keys are ignored.
71
+
72
+
#### Binding hierarchies
73
+
74
+
Configuration values can contain hierarchical data. Hierarchical objects are represented with the use of the `:` delimiter in the configuration keys. To access a configuration value, use the `:` character to delimit a hierarchy. For example, consider the following configuration values:
75
+
76
+
```json
77
+
{
78
+
"Parent": {
79
+
"FavoriteNumber": 7,
80
+
"Child": {
81
+
"Name": "Example",
82
+
"GrandChild": {
83
+
"Age": 3
84
+
}
85
+
}
86
+
}
87
+
}
88
+
```
89
+
90
+
The following table represents example keys and their corresponding values for the preceding example JSON:
91
+
92
+
| Key | Value |
93
+
|---------------------------------|-------------|
94
+
|`"Parent:FavoriteNumber"`|`7`|
95
+
|`"Parent:Child:Name"`|`"Example"`|
96
+
|`"Parent:Child:GrandChild:Age"`|`3`|
97
+
54
98
### Basic example
55
99
56
100
To access configuration values in their basic form, without the assistance of the _generic host_ approach, use the <xref:Microsoft.Extensions.Configuration.ConfigurationBuilder> type directly.
@@ -114,6 +158,18 @@ When you run this application, the `Host.CreateDefaultBuilder` defines the behav
114
158
> [!TIP]
115
159
> Using the raw `IConfiguration` instance in this way, while convenient, doesn't scale very well. When applications grow in complexity, and their corresponding configurations become more complex, we recommend that you use the [_options pattern_](options.md) as an alternative.
116
160
161
+
### Basic example with hosting and using the indexer API
162
+
163
+
Consider the same _appsettings.json_ file contents from the previous example:
The values are accessed using the indexer API where each key is a string, and the value is a string. Configuration supports properties, objects, arrays, and dictionaries.
172
+
117
173
## Configuration providers
118
174
119
175
The following table shows the configuration providers available to .NET Core apps.
@@ -130,6 +186,9 @@ The following table shows the configuration providers available to .NET Core app
|[App secrets (Secret Manager)](/aspnet/core/security/app-secrets)| File in the user profile directory |
132
188
189
+
> [!TIP]
190
+
> The order in which configuration providers are added matters. When multiple configuration providers are used and more than one provided specifies the same key, the last one added is used.
191
+
133
192
For more information on various configuration providers, see [Configuration providers in .NET](configuration-providers.md).
Copy file name to clipboardExpand all lines: docs/core/extensions/options.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,15 +3,15 @@ title: Options pattern in .NET
3
3
author: IEvangelist
4
4
description: Learn how to use the options pattern to represent groups of related settings in .NET apps.
5
5
ms.author: dapine
6
-
ms.date: 03/10/2022
6
+
ms.date: 05/12/2022
7
7
---
8
8
9
9
# Options pattern in .NET
10
10
11
11
The options pattern uses classes to provide strongly-typed access to groups of related settings. When [configuration settings](configuration.md) are isolated by scenario into separate classes, the app adheres to two important software engineering principles:
12
12
13
13
- The [Interface Segregation Principle (ISP) or Encapsulation](../../architecture/modern-web-apps-azure/architectural-principles.md#encapsulation): Scenarios (classes) that depend on configuration settings depend only on the configuration settings that they use.
14
-
-[Separation of Concerns](../../architecture/modern-web-apps-azure/architectural-principles.md#separation-of-concerns): Settings for different parts of the app aren't dependent or coupled to one another.
14
+
-[Separation of Concerns](../../architecture/modern-web-apps-azure/architectural-principles.md#separation-of-concerns): Settings for different parts of the app aren't dependent or coupled with one another.
15
15
16
16
Options also provide a mechanism to validate configuration data. For more information, see the [Options validation](#options-validation) section.
17
17
@@ -42,7 +42,7 @@ The following code is part of the _Program.cs_ C# file and:
42
42
43
43
In the preceding code, changes to the JSON configuration file after the app has started are read.
44
44
45
-
[`ConfigurationBinder.Get<T>`](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Get%2A) binds and returns the specified type. `ConfigurationBinder.Get<T>`may be more convenient than using `ConfigurationBinder.Bind`. The following code shows how to use `ConfigurationBinder.Get<T>` with the `TransientFaultHandlingOptions` class:
45
+
[`ConfigurationBinder.Get<T>`](xref:Microsoft.Extensions.Configuration.ConfigurationBinder.Get%2A) binds and returns the specified type. `ConfigurationBinder.Get<T>`maybe more convenient than using `ConfigurationBinder.Bind`. The following code shows how to use `ConfigurationBinder.Get<T>` with the `TransientFaultHandlingOptions` class:
0 commit comments