From bbb75be6fd06d7265d3c538a5c9a61ef6b46ee8e Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Fri, 24 May 2019 15:19:55 -0500 Subject: [PATCH] Configuration namespaces and line lengths (#12526) --- .../fundamentals/configuration/index.md | 37 +++++++++++-------- .../2.x/ConfigurationSample/Program.cs | 15 +++++--- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/aspnetcore/fundamentals/configuration/index.md b/aspnetcore/fundamentals/configuration/index.md index fef82a0159..4513280fdb 100644 --- a/aspnetcore/fundamentals/configuration/index.md +++ b/aspnetcore/fundamentals/configuration/index.md @@ -5,7 +5,7 @@ description: Learn how to use the Configuration API to configure an ASP.NET Core monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc -ms.date: 03/11/2019 +ms.date: 05/24/2019 uid: fundamentals/configuration/index --- # Configuration in ASP.NET Core @@ -22,12 +22,16 @@ App configuration in ASP.NET Core is based on key-value pairs established by *co * In-memory .NET objects * Settings files +Configuration packages for common configuration provider scenarios are included in the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app). Code examples that follow and in the sample app use the namespace: + +```csharp +using Microsoft.Extensions.Configuration; +``` + The *options pattern* is an extension of the configuration concepts described in this topic. Options uses classes to represent groups of related settings. For more information on using the options pattern, see . [View or download sample code](https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/fundamentals/configuration/index/samples) ([how to download](xref:index#how-to-download-a-sample)) -These three packages are included in the [Microsoft.AspNetCore.App metapackage](xref:fundamentals/metapackage-app). - ## Host vs. app configuration Before the app is configured and started, a *host* is configured and launched. The host is responsible for app startup and lifetime management. Both the app and the host are configured using the configuration providers described in this topic. Host configuration key-value pairs become part of the app's global configuration. For more information on how the configuration providers are used when the host is built and how configuration sources affect host configuration, see [The host](xref:fundamentals/index#host). @@ -97,8 +101,6 @@ Configuration providers that implement change detection have the ability to relo is available in the app's [dependency injection (DI)](xref:fundamentals/dependency-injection) container. can be injected into a Razor Pages to obtain configuration for the class: ```csharp -// using Microsoft.Extensions.Configuration; - public class IndexModel : PageModel { private readonly IConfiguration _config; @@ -163,7 +165,7 @@ This sequence of providers is put into place when you initialize a new when building the host to specify the app's configuration providers in addition to those added automatically by : -[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=19)] +[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=20)] Configuration supplied to the app in is available during the app's startup, including `Startup.ConfigureServices`. For more information, see the [Access configuration during startup](#access-configuration-during-startup) section. @@ -351,8 +353,9 @@ public class Program .ConfigureAppConfiguration((hostingContext, config) => { // Call additional providers here as needed. - // Call AddEnvironmentVariables last if you need to allow environment - // variables to override values from other providers. + // Call AddEnvironmentVariables last if you need to allow + // environment variables to override values from other + // providers. config.AddEnvironmentVariables(prefix: "PREFIX_"); }) .UseStartup(); @@ -470,7 +473,8 @@ public class Program .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()); - config.AddIniFile("config.ini", optional: true, reloadOnChange: true); + config.AddIniFile( + "config.ini", optional: true, reloadOnChange: true); }) .UseStartup(); } @@ -561,7 +565,8 @@ public class Program .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()); - config.AddJsonFile("config.json", optional: true, reloadOnChange: true); + config.AddJsonFile( + "config.json", optional: true, reloadOnChange: true); }) .UseStartup(); } @@ -628,7 +633,8 @@ public class Program .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()); - config.AddXmlFile("config.xml", optional: true, reloadOnChange: true); + config.AddXmlFile( + "config.xml", optional: true, reloadOnChange: true); }) .UseStartup(); } @@ -743,7 +749,8 @@ public class Program .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(Directory.GetCurrentDirectory()); - var path = Path.Combine(Directory.GetCurrentDirectory(), "path/to/files"); + var path = Path.Combine( + Directory.GetCurrentDirectory(), "path/to/files"); config.AddKeyPerFile(directoryPath: path, optional: true); }) .UseStartup(); @@ -831,8 +838,6 @@ The following example: * Stores the value in the `NumberConfig` property for use by the page. ```csharp -// using Microsoft.Extensions.Configuration; - public class IndexModel : PageModel { public IndexModel(IConfiguration config) @@ -1012,7 +1017,7 @@ Consider the configuration keys and values shown in the following table. These keys and values are loaded in the sample app using the Memory Configuration Provider: -[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=3-10,22)] +[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=5-12,23)] The array skips a value for index #3. The configuration binder isn't capable of binding null values or creating null entries in bound objects, which becomes clear in a moment when the result of binding this array to an object is demonstrated. @@ -1147,7 +1152,7 @@ An `AddEFConfiguration` extension method permits adding the configuration source The following code shows how to use the custom `EFConfigurationProvider` in *Program.cs*: -[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=26)] +[!code-csharp[](index/samples/2.x/ConfigurationSample/Program.cs?name=snippet_Program&highlight=30-31)] ## Access configuration during startup diff --git a/aspnetcore/fundamentals/configuration/index/samples/2.x/ConfigurationSample/Program.cs b/aspnetcore/fundamentals/configuration/index/samples/2.x/ConfigurationSample/Program.cs index 71d651b8fd..f405ceee40 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/2.x/ConfigurationSample/Program.cs +++ b/aspnetcore/fundamentals/configuration/index/samples/2.x/ConfigurationSample/Program.cs @@ -11,7 +11,8 @@ namespace ConfigurationSample #region snippet_Program public class Program { - public static Dictionary arrayDict = new Dictionary + public static Dictionary arrayDict = + new Dictionary { {"array:entries:0", "value0"}, {"array:entries:1", "value1"}, @@ -31,10 +32,14 @@ namespace ConfigurationSample { config.SetBasePath(Directory.GetCurrentDirectory()); config.AddInMemoryCollection(arrayDict); - config.AddJsonFile("json_array.json", optional: false, reloadOnChange: false); - config.AddJsonFile("starship.json", optional: false, reloadOnChange: false); - config.AddXmlFile("tvshow.xml", optional: false, reloadOnChange: false); - config.AddEFConfiguration(options => options.UseInMemoryDatabase("InMemoryDb")); + config.AddJsonFile( + "json_array.json", optional: false, reloadOnChange: false); + config.AddJsonFile( + "starship.json", optional: false, reloadOnChange: false); + config.AddXmlFile( + "tvshow.xml", optional: false, reloadOnChange: false); + config.AddEFConfiguration( + options => options.UseInMemoryDatabase("InMemoryDb")); config.AddCommandLine(args); }) .UseStartup();