/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/core/extensions/snippets/configuration/console-xml/Program.cs
48 строк
1 KB
David Pine
Apply the Minimal APIs host app builder patterns (#35922)
23 июн 2023, 20:20
Не верифицирован
23 июн 2023, 20:20
a265ac4
Код
Авторство
О чём код?
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); builder.Configuration.Sources.Clear(); builder.Configuration .AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true) .AddXmlFile("repeating-example.xml", optional: true, reloadOnChange: true); builder.Configuration.AddEnvironmentVariables(); if (args is { Length: > 0 }) { builder.Configuration.AddCommandLine(args); } IConfigurationRoot configurationRoot = builder.Configuration; string key00 = "section:section0:key:key0"; string key01 = "section:section0:key:key1"; string key10 = "section:section1:key:key0"; string key11 = "section:section1:key:key1"; string? val00 = configurationRoot[key00]; string? val01 = configurationRoot[key01]; string? val10 = configurationRoot[key10]; string? val11 = configurationRoot[key11]; Console.WriteLine($"{key00} = {val00}"); Console.WriteLine($"{key01} = {val01}"); Console.WriteLine($"{key10} = {val10}"); Console.WriteLine($"{key10} = {val11}"); using IHost host = builder.Build(); // Application code should start here. await host.RunAsync(); // <Output> // Sample output: // section:section0:key:key0 = value 00 // section:section0:key:key1 = value 01 // section:section1:key:key0 = value 10 // section:section1:key:key0 = value 11 // </Output>