Skip to content

Commit 0566cbd

Browse files
analogrelaydanroth27
authored andcommitted
update configuration samples and docs
1 parent 9df28d2 commit 0566cbd

File tree

6 files changed

+104
-23
lines changed

6 files changed

+104
-23
lines changed

NuGet.config

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<clear />
5+
<add key="aspnet1" value="https://dotnet.myget.org/F/aspnet1/api/v3/index.json" protocolVersion="3" />
6+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
7+
</packageSources>
8+
</configuration>

aspnet/fundamentals/configuration.rst

+18-23
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,11 @@ At its simplest, ``Configuration`` is just a collection of sources, which provid
2323

2424
You must configure at least one source in order for ``Configuration`` to function correctly. The following sample shows how to test working with ``Configuration`` as a key/value store:
2525

26-
.. code-block:: c#
27-
28-
var builder = new ConfigurationBuilder();
29-
builder.AddInMemoryCollection();
30-
var config = builder.Build();
31-
config["somekey"] = "somevalue";
32-
33-
// do some other work
34-
35-
var setting = config["somekey"]; // also returns "somevalue"
26+
.. literalinclude:: configuration/sample/src/CodeSnippets/ConfigSummarySnippet.cs
27+
:language: c#
28+
:dedent: 12
29+
:start-after: // SNIPPET-START
30+
:end-before: // SNIPPET-END
3631

3732
.. note:: You must set at least one configuration source.
3833

@@ -43,7 +38,7 @@ It's not unusual to store configuration values in a hierarchical structure, espe
4338
.. literalinclude:: /../common/samples/WebApplication1/src/WebApplication1/appsettings.json
4439
:language: json
4540

46-
The application uses configuration to configure the right connection string. Access to the ``DefaultConnection`` setting is achieved through this key: ``ConnectionStrings:DefaultConnection``, or by using the ``GetConnectionString("DefaultConnection")`` extension method.
41+
The application uses configuration to configure the right connection string. Access to the ``DefaultConnection`` setting is achieved through this key: ``ConnectionStrings:DefaultConnection``, or by using the :dn:method:`~Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString` extension method and passing in ``"DefaultConnection"``.
4742

4843
The settings required by your application and the mechanism used to specify those settings (configuration being one example) can be decoupled using the :ref:`options pattern <options-config-objects>`. To use the options pattern you create your own options class (probably several different classes, corresponding to different cohesive groups of settings) that you can inject into your application using an options service. You can then specify your settings using configuration or whatever mechanism you choose.
4944

@@ -54,7 +49,7 @@ Using the built-in sources
5449

5550
The configuration framework has built-in support for JSON, XML, and INI configuration files, as well as support for in-memory configuration (directly setting values in code) and the ability to pull configuration from environment variables and command line parameters. Developers are not limited to using a single configuration source. In fact several may be set up together such that a default configuration is overridden by settings from another source if they are present.
5651

57-
Adding support for additional configuration sources is accomplished through extension methods. These methods can be called on a ``ConfigurationBuilder`` instance in a standalone fashion, or chained together as a fluent API. Both of these approaches are demonstrated in the sample below.
52+
Adding support for additional configuration sources is accomplished through extension methods. These methods can be called on a :dn:class:`~Microsoft.Extensions.Configuration.ConfigurationBuilder` instance in a standalone fashion, or chained together as a fluent API. Both of these approaches are demonstrated in the sample below.
5853

5954
.. _custom-config:
6055

@@ -75,9 +70,9 @@ It can be useful to have environment-specific configuration files. This can be a
7570
:lines: 20-35
7671
:emphasize-lines: 6
7772

78-
The ``IHostingEnvironment`` service is used to get the current environment. In the ``Development`` environment, the highlighted line of code above would look for a file named ``appsettings.Development.json`` and use its values, overriding any other values, if it's present. Learn more about :doc:`environments`.
73+
The :dn:iface:`~Microsoft.AspNetCore.Hosting.IHostingEnvironment` service is used to get the current environment. In the ``Development`` environment, the highlighted line of code above would look for a file named ``appsettings.Development.json`` and use its values, overriding any other values, if it's present. Learn more about :doc:`environments`.
7974

80-
When specifying files as configuration sources, you can optionally specify whether changes to the file should result in the settings being reloaded. This is configured by passing in a ``true`` value for the ``reloadOnChange`` parameter when calling ``AddJsonFile`` or similar file-based extension methods.
75+
When specifying files as configuration sources, you can optionally specify whether changes to the file should result in the settings being reloaded. This is configured by passing in a ``true`` value for the ``reloadOnChange`` parameter when calling :dn:method:`~Microsoft.Extensions.Configuration.JsonConfigurationExtensions.AddJsonFile` or similar file-based extension methods.
8176

8277
.. warning:: You should never store passwords or other sensitive data in configuration provider code or in plain text configuration files. You also shouldn't use production secrets in your development or test environments. Instead, such secrets should be specified outside the project tree, so they cannot be accidentally committed into the configuration provider repository. Learn more about :doc:`environments` and managing :doc:`/security/app-secrets`.
8378

@@ -108,7 +103,7 @@ A simple ``MyOptions`` class is shown here:
108103
:lines: 3-7
109104
:dedent: 4
110105

111-
Options can be injected into your application using the ``IOptions<TOptions>`` accessor service. For example, the following :doc:`controller </mvc/controllers/index>` uses ``IOptions<MyOptions>`` to access the settings it needs to render the ``Index`` view:
106+
Options can be injected into your application using the :dn:iface:`~Microsoft.Extensions.Options.IOptions\<TOptions>` accessor service. For example, the following :doc:`controller </mvc/controllers/index>` uses ``IOptions<MyOptions>`` to access the settings it needs to render the ``Index`` view:
112107

113108
.. literalinclude:: configuration/sample/src/UsingOptions/Controllers/HomeController.cs
114109
:language: c#
@@ -118,7 +113,7 @@ Options can be injected into your application using the ``IOptions<TOptions>`` a
118113

119114
.. tip:: Learn more about :doc:`dependency-injection`.
120115

121-
To setup the ``IOptions<TOption>`` service you call the ``AddOptions()`` extension method during startup in your ``ConfigureServices`` method:
116+
To setup the :dn:iface:`~Microsoft.Extensions.Options.IOptions\<TOptions>` service you call the :dn:method:`~Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.AddOptions` extension method during startup in your ``ConfigureServices`` method:
122117

123118
.. literalinclude:: configuration/sample/src/UsingOptions/Startup.cs
124119
:language: c#
@@ -132,7 +127,7 @@ The ``Index`` view displays the configured options:
132127

133128
.. image:: configuration/_static/index-view.png
134129

135-
You configure options using the ``Configure<TOption>`` extension method. You can configure options using a delegate or by binding your options to configuration:
130+
You configure options using the :dn:method:`~Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure\<TOptions>` extension method. You can configure options using a delegate or by binding your options to configuration:
136131

137132
.. literalinclude:: configuration/sample/src/UsingOptions/Startup.cs
138133
:language: c#
@@ -142,16 +137,16 @@ You configure options using the ``Configure<TOption>`` extension method. You can
142137

143138
When you bind options to configuration, each property in your options type is bound to a configuration key of the form ``property:subproperty:...``. For example, the ``MyOptions.Option1`` property is bound to the key ``Option1``, which is read from the ``option1`` property in *appsettings.json*. Note that configuration keys are case insensitive.
144139

145-
Each call to ``Configure<TOption>`` adds an ``IConfigureOptions<TOption>`` service to the service container that is used by the ``IOptions<TOption>`` service to provide the configured options to the application or framework. If you want to configure your options some other way (for example, reading settings from a database) you can use the ``AddSingleton<TOptions>`` extension method to you specify a custom ``IConfigureOptions<TOption>`` service directly.
140+
Each call to :dn:method:`~Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure\<TOptions>` adds an :dn:iface:`~Microsoft.Extensions.Options.IConfigureOptions\<TOptions>` service to the service container that is used by the :dn:iface:`~Microsoft.Extensions.Options.IOptions\<TOptions>` service to provide the configured options to the application or framework. If you want to configure your options some other way (for example, reading settings from a database) you can use the ``AddSingleton<TOptions>`` extension method to you specify a custom :dn:iface:`~Microsoft.Extensions.Options.IConfigureOptions\<TOptions>` service directly.
146141

147-
You can have multiple ``IConfigureOptions<TOption>`` services for the same option type and they are all applied in order. In the :ref:`example <options-example>` above, the values of ``Option1`` and ``Option2`` are both specified in `appsettings.json`, but the value of ``Option1`` is overridden by the configured delegate with the value "value1_from_action".
142+
You can have multiple :dn:iface:`~Microsoft.Extensions.Options.IConfigureOptions\<TOptions>` services for the same option type and they are all applied in order. In the :ref:`example <options-example>` above, the values of ``Option1`` and ``Option2`` are both specified in `appsettings.json`, but the value of ``Option1`` is overridden by the configured delegate with the value "value1_from_action".
148143

149144
.. _custom-config-providers:
150145

151146
Writing custom providers
152147
------------------------
153148

154-
In addition to using the built-in configuration providers, you can also write your own. To do so, you simply implement the ``IConfigurationSource`` interface, which exposes a ``Build`` method. The build method configures and returns an ``IConfigurationProvider``.
149+
In addition to using the built-in configuration providers, you can also write your own. To do so, you simply implement the :dn:iface:`~Microsoft.Extensions.Configuration.IConfigurationSource` interface, which exposes a :dn:method:`~Microsoft.Extensions.Configuration.IConfigurationSource.Build` method. The build method configures and returns an :dn:iface:`~Microsoft.Extensions.Configuration.IConfigurationProvider`.
155150

156151
Example: Entity Framework Settings
157152
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -173,13 +168,13 @@ You need a ``ConfigurationContext`` to store and access the configured values us
173168
:dedent: 4
174169
:emphasize-lines: 7
175170

176-
Create an ``EntityFrameworkConfigurationSource`` that inherits from ``IConfigurationSource``:
171+
Create an ``EntityFrameworkConfigurationSource`` that inherits from :dn:iface:`~Microsoft.Extensions.Configuration.IConfigurationSource`:
177172

178173
.. literalinclude:: configuration/sample/src/CustomConfigurationProvider/EntityFrameworkConfigurationSource.cs
179174
:language: c#
180175
:emphasize-lines: 7,16-19
181176

182-
Next, create the custom configuration provider by inheriting from ``ConfigurationProvider``. The configuration data is loaded by overriding the ``Load`` method, which reads in all of the configuration data from the configured database. For demonstration purposes, the configuration provider also takes care of initializing the database if it hasn't already been created and populated:
177+
Next, create the custom configuration provider by inheriting from :dn:class:`~Microsoft.Extensions.Configuration.ConfigurationProvider`. The configuration data is loaded by overriding the ``Load`` method, which reads in all of the configuration data from the configured database. For demonstration purposes, the configuration provider also takes care of initializing the database if it hasn't already been created and populated:
183178

184179
.. literalinclude:: configuration/sample/src/CustomConfigurationProvider/EntityFrameworkConfigurationProvider.cs
185180
:language: c#
@@ -193,7 +188,7 @@ By convention you can also add an ``AddEntityFrameworkConfiguration`` extension
193188
:language: c#
194189
:emphasize-lines: 9
195190

196-
You can see an example of how to use this custom configuration provider in your application in the following example. Create a new ``ConfigurationBuilder`` to set up your configuration sources. To add the ``EntityFrameworkConfigurationProvider``, you first need to specify the EF data provider and connection string. How should you configure the connection string? Using configuration of course! Add an *appsettings.json* file as a configuration source to bootstrap setting up the ``EntityFrameworkConfigurationProvider``. By adding the database settings to an existing configuration with other sources specified, any settings specified in the database will override settings specified in *appsettings.json*:
191+
You can see an example of how to use this custom configuration provider in your application in the following example. Create a new :dn:class:`~Microsoft.Extensions.Configuration.ConfigurationBuilder` to set up your configuration sources. To add the ``EntityFrameworkConfigurationProvider``, you first need to specify the EF data provider and connection string. How should you configure the connection string? Using configuration of course! Add an *appsettings.json* file as a configuration source to bootstrap setting up the ``EntityFrameworkConfigurationProvider``. By adding the database settings to an existing configuration with other sources specified, any settings specified in the database will override settings specified in *appsettings.json*:
197192

198193
.. literalinclude:: configuration/sample/src/CustomConfigurationProvider/Program.cs
199194
:language: c#
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>3476e7ae-4377-44f2-b33d-b45cdede1481</ProjectGuid>
11+
<RootNamespace>CodeSnippets</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.Extensions.Configuration;
2+
3+
namespace CodeSnippets
4+
{
5+
public class ConfigSummarySnippet
6+
{
7+
public ConfigSummarySnippet()
8+
{
9+
// Everything between SNIPPET-START and SNIPPET-END will be inlined in the doc!
10+
// SNIPPET-START
11+
var builder = new ConfigurationBuilder();
12+
builder.AddInMemoryCollection();
13+
var config = builder.Build();
14+
config["somekey"] = "somevalue";
15+
16+
// do some other work
17+
18+
var setting = config["somekey"]; // also returns "somevalue"
19+
// SNIPPET-END
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyConfiguration("")]
9+
[assembly: AssemblyCompany("")]
10+
[assembly: AssemblyProduct("CodeSnippets")]
11+
[assembly: AssemblyTrademark("")]
12+
13+
// Setting ComVisible to false makes the types in this assembly not visible
14+
// to COM components. If you need to access a type in this assembly from
15+
// COM, set the ComVisible attribute to true on that type.
16+
[assembly: ComVisible(false)]
17+
18+
// The following GUID is for the ID of the typelib if this project is exposed to COM
19+
[assembly: Guid("3476e7ae-4377-44f2-b33d-b45cdede1481")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "1.0.0-*",
3+
4+
"dependencies": {
5+
"NETStandard.Library": "1.6.0",
6+
"Microsoft.Extensions.Configuration": "1.0.0",
7+
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0",
8+
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0"
9+
},
10+
11+
"frameworks": {
12+
"netstandard1.5": {
13+
"imports": "dnxcore50"
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)