Skip to content

Commit f5ba022

Browse files
authored
Add ConfigurationBuilder example (dotnet#7646)
1 parent 3ca2901 commit f5ba022

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
12+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="appsettings.json">
17+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.Extensions.Configuration;
2+
3+
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json", false, true);
4+
IConfigurationRoot root = builder.Build();
5+
6+
Console.WriteLine($"Hello, { root["weather"] } world!");
7+
8+
/* This program outputs the following text:
9+
*
10+
* Hello, stormy world!
11+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"weather": "stormy"
3+
}

xml/Microsoft.Extensions.Configuration/ConfigurationBuilder.xml

+29-7
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,29 @@
2626
</Interface>
2727
</Interfaces>
2828
<Docs>
29-
<summary>Used to build key/value based configuration settings for use in an application.</summary>
30-
<remarks>To be added.</remarks>
29+
<summary>Used to build key/value-based configuration settings for use in an application.</summary>
30+
<remarks>
31+
<format type="text/markdown"><![CDATA[
32+
33+
## Remarks
34+
35+
By using this class, you can store configuration values in a JSON file, for example, and then retrieve them at run time. For more information, see [Configuration - basic example](/dotnet/core/extensions/configuration#basic-example).
36+
37+
If you're migrating an app to .NET Core 3.1 or a later version, you can use this class to replace the *app.config* file functionality that you may have previously used to configure your app.
38+
39+
## Example
40+
41+
The following code snippet shows a simplified app that creates a `ConfigurationBuilder` object to retrieve a string value from a JSON file at run time.
42+
43+
:::code language="csharp" source="~/snippets/csharp/Microsoft.Extensions.Configuration/ConfigurationBuilder/Overview/Program.cs":::
44+
45+
The contents of the JSON file are shown here.
46+
47+
:::code language="json" source="~/snippets/csharp/Microsoft.Extensions.Configuration/ConfigurationBuilder/Overview/appsettings.json":::
48+
49+
]]></format>
50+
</remarks>
51+
<related type="Article" href="/dotnet/core/extensions/configuration">Configuration in .NET</related>
3152
</Docs>
3253
<Members>
3354
<Member MemberName=".ctor">
@@ -51,7 +72,7 @@
5172
</AssemblyInfo>
5273
<Parameters />
5374
<Docs>
54-
<summary>To be added.</summary>
75+
<summary>Initializes a new instance of the <see cref="T:Microsoft.Extensions.Configuration.ConfigurationBuilder" /> class.</summary>
5576
<remarks>To be added.</remarks>
5677
</Docs>
5778
</Member>
@@ -159,8 +180,9 @@
159180
</ReturnValue>
160181
<Docs>
161182
<summary>Gets a key/value collection that can be used to share data between the <see cref="T:Microsoft.Extensions.Configuration.IConfigurationBuilder" />
162-
and the registered <see cref="T:Microsoft.Extensions.Configuration.IConfigurationProvider" />s.</summary>
163-
<value>To be added.</value>
183+
and the registered configuration providers.</summary>
184+
<value>A key/value collection that can be used to share data between the <see cref="T:Microsoft.Extensions.Configuration.IConfigurationBuilder" />
185+
and the registered configuration providers.</value>
164186
<remarks>To be added.</remarks>
165187
</Docs>
166188
</Member>
@@ -197,8 +219,8 @@
197219
<ReturnType FrameworkAlternate="dotnet-plat-ext-2.0;dotnet-plat-ext-2.1;dotnet-plat-ext-2.2;dotnet-plat-ext-3.0;dotnet-plat-ext-3.1;dotnet-plat-ext-5.0;dotnet-plat-ext-6.0">System.Collections.Generic.IList&lt;Microsoft.Extensions.Configuration.IConfigurationSource&gt;</ReturnType>
198220
</ReturnValue>
199221
<Docs>
200-
<summary>Returns the sources used to obtain configuration values.</summary>
201-
<value>To be added.</value>
222+
<summary>Gets the sources used to obtain configuration values.</summary>
223+
<value>The sources used to obtain configuration values.</value>
202224
<remarks>To be added.</remarks>
203225
</Docs>
204226
</Member>

0 commit comments

Comments
 (0)