From f5d89afebd954937567beaf40fc8052043fa9bae Mon Sep 17 00:00:00 2001 From: koenigst Date: Wed, 11 Sep 2024 15:46:02 +0200 Subject: [PATCH] Corrected the generic host configuration sample based on feedback. (#42582) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Corrected the generic host configuration sample based on feedback in dotnet/runtime#97930 * Update generic-host.md * Update docs/core/extensions/snippets/configuration/console-host/Program.cs --------- Co-authored-by: Stefan König Co-authored-by: David Pine --- docs/core/extensions/generic-host.md | 6 +++--- .../configuration/console-host/Program.cs | 16 +++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/docs/core/extensions/generic-host.md b/docs/core/extensions/generic-host.md index 906a5e8021595..f9a040858984b 100644 --- a/docs/core/extensions/generic-host.md +++ b/docs/core/extensions/generic-host.md @@ -3,7 +3,7 @@ title: .NET Generic Host author: IEvangelist description: Learn about the .NET Generic Host, which is responsible for app startup and lifetime management. ms.author: dapine -ms.date: 05/22/2024 +ms.date: 09/11/2024 --- # .NET Generic Host @@ -196,11 +196,11 @@ Host configuration is used to configure properties of the [IHostEnvironment](#ih # [IHostApplicationBuilder](#tab/appbuilder) -The host configuration is available in property and the environment implementation is available in property. To configure the host, access the `Configuration` property and call any of the available extension methods. +The host configuration is available in property and the environment implementation is available in property. To configure the host, access the `Configuration` property and call any of the available extension methods. To add host configuration, consider the following example: -:::code language="csharp" source="snippets/configuration/console-host/Program.cs" highlight="6-9"::: +:::code language="csharp" source="snippets/configuration/console-host/Program.cs" highlight="6-8"::: The preceding code: diff --git a/docs/core/extensions/snippets/configuration/console-host/Program.cs b/docs/core/extensions/snippets/configuration/console-host/Program.cs index d02925c390a12..beeb282e6a6e8 100644 --- a/docs/core/extensions/snippets/configuration/console-host/Program.cs +++ b/docs/core/extensions/snippets/configuration/console-host/Program.cs @@ -1,12 +1,18 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); +HostApplicationBuilderSettings settings = new() +{ + Args = args, + Configuration = new ConfigurationManager(), + ContentRootPath = Directory.GetCurrentDirectory(), +}; -builder.Environment.ContentRootPath = Directory.GetCurrentDirectory(); -builder.Configuration.AddJsonFile("hostsettings.json", optional: true); -builder.Configuration.AddEnvironmentVariables(prefix: "PREFIX_"); -builder.Configuration.AddCommandLine(args); +settings.Configuration.AddJsonFile("hostsettings.json", optional: true); +settings.Configuration.AddEnvironmentVariables(prefix: "PREFIX_"); +settings.Configuration.AddCommandLine(args); + +HostApplicationBuilder builder = Host.CreateApplicationBuilder(settings); using IHost host = builder.Build();