Skip to content

Commit

Permalink
Corrected the generic host configuration sample based on feedback. (#…
Browse files Browse the repository at this point in the history
…42582)

* 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 <[email protected]>
Co-authored-by: David Pine <[email protected]>
  • Loading branch information
3 people committed Sep 11, 2024
1 parent 6ff48c3 commit f5d89af
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docs/core/extensions/generic-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -196,11 +196,11 @@ Host configuration is used to configure properties of the [IHostEnvironment](#ih

# [IHostApplicationBuilder](#tab/appbuilder)

The host configuration is available in <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder.Configuration?displayProperty=nameWithType> property and the environment implementation is available in <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder.Environment?displayProperty=nameWithType> property. To configure the host, access the `Configuration` property and call any of the available extension methods.
The host configuration is available in <xref:Microsoft.Extensions.Hosting.HostApplicationBuilderSettings.Configuration?displayProperty=nameWithType> property and the environment implementation is available in <xref:Microsoft.Extensions.Hosting.IHostApplicationBuilder.Environment?displayProperty=nameWithType> 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:

Expand Down
Original file line number Diff line number Diff line change
@@ -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();

Expand Down

0 comments on commit f5d89af

Please sign in to comment.